简体   繁体   English

雄辩-试图获取非对象的属性

[英]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.

相关问题 Laravel 4雄辩地试图获得非对象的属性 - Laravel 4 Eloquent Trying to get property of non-object 雄辩的关系控制器投身于试图获取非对象的属性 - Eloquent Relationship Controller throw to Trying to get property of non-object 试图在 laravel eloquent 中获取非对象错误的属性“名称” - Trying to get property 'name' of non-object error in laravel eloquent Eloquent Laravel show试图获取非对象的属性“代码” - Eloquent Laravel show Trying to get property 'code' of non-object 尝试使用雄辩的关系时尝试获取非对象错误的属性 - Trying to get property of non-object error while trying to use eloquent relationship 试图获取非对象的属性,但这是一个对象 - Trying to get property of non-object, but it is an object Laravel:访问Eloquent对象的属性会导致“尝试获取非对象属性”错误 - Laravel : Accessing attributes of an Eloquent object results in 'Trying to get property of non-object' error laravel eloquent foreach 循环错误尝试获取非对象的属性“location_id” - laravel eloquent foreach loop error Trying to get property 'location_id' of non-object Laravel Eloquent 关系返回试图获取非对象的属性“office_name” - Laravel Eloquent relationship return Trying to get property 'office_name' of non-object 错误 Laravel 5.8 Eloquent - 试图获取非对象的属性“名称” - Error Laravel 5.8 Eloquent - Trying to get property 'name' of non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM