Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alnahian2003's avatar

How to check if a model has been updated using `updateOrFail()` method?

Hey everyone! I'm a big fan of Laravel's {update/delete}orFail() methods and I'm successfully updating some of my models using the updateOrFail method.

But as I'm using Spatie's Laravel Permissions package, I also want to sync some roles and permissions based on successful updates of some models. I'm aware of eloquent's isDirty() and wasChanged() methods but seems like they're not working as I'm mass-updating my models.

What should I do now?

Here's a snippet of my current update() method in the controller:

public function update(UpdateUserRequest $request, User $user): mixed
    {
        $user->updateOrFail(
            [
                'name' => $request->name,
                'email' => $request->email,
				.
				.
				...
            ]
        );

		// This is where I want to put a check
        $user->syncRoles($request->role);

        flash()->addInfo('The user has been updated successfully!');
        return to_route('users.index');
    }

THANK YOU, HAVE A GREAT DAY!

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

The updateOrFail method returns boolean or throws an exception, so if you are at the line of sync it means that it already worked, however you can do this too:

$result =  $user->updateOrFail(
            [
                'name' => $request->name,
                'email' => $request->email,
				.
				.
				...
            ]
        );

if ($result)
{
	$user->syncRoles...
}
1 like

Please or to participate in this conversation.