[英]Eloquent - Trying to get property of non-object
I am using Laravel 5.5
and I am having the following query: 我正在使用
Laravel 5.5
并且有以下查询:
$instru = Instruments::where('name', '=', $coinArr[$key])->first();
$i = Revision::where('id', '=', $instru->revisions_id)->first();
if ($i === NULL) { ...
In the setup case $instru = NULL
hence the query on the Revision
model does not work. 在设置情况下,
$instru = NULL
因此对Revision
模型的查询不起作用。
My problem is that I if I var_dump($i)
I get the following error and do not run into the if-clause
. 我的问题是,如果我使用
var_dump($i)
,则会收到以下错误,并且不会遇到if-clause
。
[2017-12-26 21:38:10] local.ERROR: Trying to get property of non-object
Any suggestions how to deal with this case? 有什么建议如何处理这种情况?
You should change: 您应该更改:
$i = Revision::where('id', '=', $instru->revisions_id)->first();
into: 成:
$i = $instru ? Revision::where('id', '=', $instru->revisions_id)->first() : null;
This is because in some cases $instru
is null and you cannot run $instru->revisions_id
when $instru
is null 这是因为在某些情况下
$instru
为空,并且当$instru
为空时,您将无法运行$instru->revisions_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.