I'm trying to validate the update method in laravel 5.4 Following is my validation code:
$data = $request->only('id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client');
$company = Companies::find($request->id);
Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required', Rule::unique('companies')->ignore($company->id),
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
]);
But this validation breaks, suppose in update method I pass name empty it not throwing error code 422
It is showing this error and I'm unable to catch the error and display in span tag
Update
As suggested in answers I tried adding the validate and trying to check I'm getting following error:
Trying to get property of non-object
Following is my updated code:
$data = $request->only(['id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client']);
$company = Companies::find($request->id);
Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required', Rule::unique('companies')->ignore($company->id),
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
])->validate();
Even if I don't make my $data = request->only()
an array I get the same error.
You are only creating Validator
, not validating the data. Have to call validate
function.
Validator::make($data, [
...
])->validate();
UPDATE
For error Trying to get property of non-object
. Notice you are passing email validation rules as string where it should be array
'email' => ['required', Rule::unique('companies')->ignore($company->id)],
If error still exists. Check the variable $company
if its contains data.