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

2019-03-17 11:38发布

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!

2条回答
forever°为你锁心
2楼-- · 2019-03-17 11:46

If you just want to escape without knowing what is happening you can try this,

 $user = new User;
    $user->fields = 'example';
    if($user->save()){
        return true;
    }
    else{
        // returns false
        //do something
    } 
查看更多
聊天终结者
3楼-- · 2019-03-17 11:49

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 :)

查看更多
登录 后发表回答