如何解决 Laravel 上的“试图获取非对象的属性”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44200950/
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
How to solve Error "Trying to get property of non-object" on Laravel
提问by Bireon
I am trying to get the list from the Database, but the page showed the error "Trying to get property of non-object" please help me, I got stacked on this, here the code make error: $status = ($employee->Profile->date_of_leaving == null) ? '<span class="label label-success">active</span>' : '<span class="label label-danger">in-active</span>';
我正在尝试从数据库中获取列表,但页面显示错误“试图获取非对象的属性”,请帮助我,我对此感到困惑,这里的代码出错: $status = ($employee->Profile->date_of_leaving == null) ? '<span class="label label-success">active</span>' : '<span class="label label-danger">in-active</span>';
and below is the extended code of the file it self.
下面是文件本身的扩展代码。
foreach ($employees as $employee){
$designation = $employee->Designation;
$status = ($employee->Profile->date_of_leaving == null) ? '<span class="label label-success">active</span>' : '<span class="label label-danger">in-active</span>';
$linkToEdit = "<a href='employee/$employee->id/edit' class='DTTT_button_small'> <i class='fa fa-edit'></i></a>";
$linkToDelete = "<a href='employee/$employee->id/delete/$token' class='DTTT_button_small alert_delete'> <i class='fa fa-trash-o'></i></a>";
$linkToView = "<a href='employee/$employee->id' class='DTTT_button_small'> <i class='fa fa-share'></i></a>";
$Option = "$linkToView $linkToEdit $linkToDelete";
$col_data[] = array(
($employee->Profile->employee_code != '') ? $employee->Profile->employee_code : 'Not assigned' ,
$employee->first_name,
$employee->last_name,
$employee->username,
$employee->email,
$designation->designation,
$status,
$Option);
}
采纳答案by Mohammad Hamedani
When you call property of an variable that not object (maybe null, string or other types), this error has been occured. In this code you try to get date_of_leaving
property of $employee->Profile
and $employee->Profile
is not object. So you can check object has that property by calling property_exists
function:
当你调用一个非对象变量的属性(可能是空值、字符串或其他类型)时,就会出现这个错误。在此代码试图获得date_of_leaving
的财产$employee->Profile
,并$employee->Profile
没有对象。因此,您可以通过调用property_exists
函数来检查对象是否具有该属性:
$status = (property_exists($employee, 'Profile') && property_exists($employee->Profile, 'date_of_leaving') && $employee->Profile->date_of_leaving == null) ? '<span class="label label-success">active</span>' : '<span class="label label-danger">in-active</span>';
And this line:
而这一行:
(property_exists($employee, 'Profile') && property_exists($employee->Profile, 'employee_code') && $employee->Profile->employee_code != '') ? $employee->Profile->employee_code : 'Not assigned' ,
回答by Robin Choffardet
The error you mentionned tell us that you're trying to get a property of a variable which is not an object.
您提到的错误告诉我们您正在尝试获取不是对象的变量的属性。
The line of code which produces the error is :
($employee->Profile->date_of_leaving == null)
产生错误的代码行是:
($employee->Profile->date_of_leaving == null)
Both $employee
and $employee->Profile
could return null
but in your code, just before this line, you wrote : $designation = $employee->Designation;
which would have failed if $employee
was not an object.
双方$employee
并$employee->Profile
可以返回null
,但在你的代码,只是这一行之前,你写道:$designation = $employee->Designation;
如果这将失败$employee
不是一个对象。
By elimination $employee->Profile
is the reasons why you script fails. It may is not set or not an object :)
通过消除$employee->Profile
是脚本失败的原因。它可能未设置或不是对象:)
回答by Pawan Dongol
This error is due to having no data in database but you are trying to fetch it so you need to check it's null or not in each one. I think this code help you.
此错误是由于数据库中没有数据,但您正在尝试获取它,因此您需要检查每个数据是否为空。我认为这段代码可以帮助你。
$status = (($employee->Profile) && ($employee->Profile->date_of_leaving)) ?
'<span class="label label-danger">in-active</span>' :
'<span class="label label-success">active</span>';