Home » Php » php – Laravel Model->save() returns false but no error

php – Laravel Model->save() returns false but no error

Posted by: admin April 23, 2020 Leave a comment

Questions:

Normally when I call Model->save(), it creates the new record in the database successfully. I’m trying to debug a situation when nothing happens and Model->save() returns false. How do I find out what’s happening?

$user = new User;
$user->fields = 'example';
$user->save(); // returns false

Running this does not show any insert queries.

dd(DB::getQueryLog());

But if I var_dump($user), I correctly get all the fields properly saved in the object.

Thanks!

How to&Answers:

To get the insert queries when $user->save(); error, you can try to catch the exception like this:

    try{
       $user = new User;
       $user->fields = 'example';
       $user->save(); // returns false
    }
    catch(\Exception $e){
       // do task when error
       echo $e->getMessage();   // insert query
    }

Hope this helps 🙂