Laravel Model-> save()返回false但没有错误 [英] Laravel Model->save() returns false but no error

查看:896
本文介绍了Laravel Model-> save()返回false但没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当我调用Model->save()时,它将成功在数据库中创建新记录.我正在尝试调试什么都没有发生并且Model->save()返回false的情况.我怎么知道发生了什么事?

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());

但是如果我var_dump($user),我可以正确地将所有字段正确保存在对象中.

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

谢谢!

推荐答案

要在$user->save();错误时获取插入查询,可以尝试捕获如下异常:

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
    }

希望这会有所帮助:)

这篇关于Laravel Model-> save()返回false但没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆