Rafael
Rafael

Reputation: 21

FatalThrowableError in Laravel

I get an error

Call to undefined method Illuminate\Auth\GenericUser::update()

here is code

$user = Auth::user();

$user->name = 'name';
$user->update();

return redirect()->back();

Upvotes: 1

Views: 10943

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You should check if user is authenticated, then you can update name:

if (auth()->check()) {
    auth()->user()->update(['name' => 'name']);
} else {
    dd('User is not authenticated');
}

Upvotes: 1

Govind Samrow
Govind Samrow

Reputation: 10179

You need update user from User model and then update Auth onject

$user = User::find(Auth::user()->id);
$user->name = 'name';
$user->save();

Update Auth:

Auth::setUser($user);

Upvotes: 2

Related Questions