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

davidrushton's avatar

Help: Eloquent save() stopped working

Hey. I wonder if anyone can help me - I'd be super grateful

My $user->save() has suddenly stopped working on my live server, but when testing it works locally and on dev/staging. Assuming it's a database issue, I've also tried cloning my live DB onto local, but I can't replicate.

My repository class does

if ( ! $user->save() ) {
    throw new StorageException('Could not create user');
}

And on my live server i’ve started catching those exceptions every time the account / user edit page is submitted and the User model is saved.

Nothing else untoward in the logs, var_dump($user->save()) does indeed return false, and I've restarted nginx and mysql with no joy.

Any ideas?

0 likes
7 replies
jekinney's avatar

Is it saving to the database? I have never used $user->save() in an if statement because that code shouldn't exacute just evaluate to true or false. I always pass the object in (this case $user) after it's created. If it isn't saved then it will evaluate to false. If a new object was created it should evaluate to true.

davidrushton's avatar

I've written the following test:

$user = User::find(6);

var_dump($user->save());

$user->firstname = 'David';

var_dump($user->save());

$user->firstname = 'Different';

var_dump($user->save());

which returns

boolean false
boolean false
boolean true

So, it would appear Eloquent returns false when nothing was changed from the original model.

I think this is new behaviour on Eloquent but i'll keep looking into it.

Erik's avatar

What does it return if you do the following?

$user = User::find(6);
$user->firstname = 'David';
$user->save();

dd($user);
sukonovs's avatar

Why are you wrapping save method in if statement? It will not return false in 99% of times. If something will go wrong, PDOException will be thrown.

Save will return false only if "saving" event will return false, and that will never happen if you specially don't do it yourself.

davidrushton's avatar

Hey, @sukonovs.

It's a practice I follow in all my repositories, throwing a StorageException if I can't guarantee the model was saved, therefore halting any code that expects there to be something to to work with (saving relationships, redirecting to ID, etc).

I understand that a PDOException would be thrown for MySQL errors, but in this case save was returning false so my code caught this and I was alerted.

Please or to participate in this conversation.