【发布时间】:2019-10-05 09:10:24
【问题描述】:
我有带有字段的 User 模型:
id
name
email
password
为什么 Laravel 在请求不存在的属性期间不返回错误。请求不存在的属性时如何返回错误?
代码:
$user = User::findOrFail(1);
echo $user->name; // This attribute exist in out table and we can continue...
echo $user->location; // Attribute `location` doesn't defined and we can't continue!!!
【问题讨论】:
-
它将返回null,您可以使用
is_null检查并根据需要返回错误。 -
如何在错误处理程序
(Exceptions\Handler.php)上为所有模型执行此操作? @PrafullaKumarSahu -
这不是例外,所以我认为您不会将其作为例外处理,但是您在这种情况下面临的问题是什么,请告诉我,也许我可以给出其他看法给它。
-
我只希望使用我的应用程序 API 的用户不要请求不存在的表字段。 @PrafullaKumarSahu
-
您能否展示您的用户如何能够请求不同的表格字段?您应该为此实际使用验证。
标签: laravel exception eloquent laravel-5.8