php Laravel Model->save() 返回 false 但没有错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29759978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Model->save() returns false but no error
提问by Lyon
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?
通常当我调用时Model->save()
,它会成功地在数据库中创建新记录。我正在尝试调试没有任何反应并Model->save()
返回 false 的情况。我如何知道发生了什么?
$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.
但是,如果我var_dump($user)
正确地将所有字段正确保存在对象中。
Thanks!
谢谢!
回答by Eng Cy
To get the insert queries when $user->save();
error, you can try to catch the exception like this:
要在$user->save();
出错时获取插入查询,您可以尝试像这样捕获异常:
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 :)
希望这可以帮助 :)