I have a custom validator:
$validator = Validator::make($request->all(), [
'name' => 'sometimes|unique:stock.containers|max:255',
]);
I would like to throw an error if I do:
PUT:localhost/my/model?description=foobar
Because I only want to accept name in this case. Then I would ensure that my $validator->valid() only contain accepted fields.
Here is my Controller:
class ContainerController extends Controller
{
public function update($id, Request $request) {
$container = Container::find($id);
$rules = ['name' => 'max:255'];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json($validator->errors(), 422); //i'm not getting any
}
$container->update($validator->valid());
}
}
And with the above request, I reach the $container->update. I would like to restrict the field to those mentioned in my rules.