0
votes

I'm using Laravel as an API with Passport Password Grant Token. I need to validate user attributes from the request but I'm getting an error with validate() method.

My update method below...

public function update(Request $request)
{
    $attributes = [
        'last_name' => ['string'],
        'first_name' => ['string']
    ];
    $validator = $request->user->validate($attributes);

    if ($validator->fails()) {
        return $validator;
    }
}

...is returning this error log:

local.ERROR: Call to a member function validate() on array {"userId":1234,"exception":"[object] (Error(code: 0): Call to a member function validate() on array at /Users/me/my-project/app/Http/Controllers/UserController.php:122)

What's wrong in my code ?

1
This line throw the error ` return $validator;`?sta
@Droid $request->user->validate($attributes) returns the errorDevonDahon

1 Answers

0
votes

to get the current user from request I think you should use user() method not user property ...

anyway .. to get validator from an array with rules, you can use Validator::make method:

   $validator = Validator::make($request->all(),  $attributes);

  if ($validator->fails()) {
        return $validator; // here I think you should redirect to error page
    }