Bireon
Bireon

Reputation: 189

How to solve Error "Trying to get property of non-object" on Laravel

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>';

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);    
        }

Upvotes: 2

Views: 16663

Answers (4)

Okechukwu Malcolm
Okechukwu Malcolm

Reputation: 37

this error is actually caused by an 'internal server error' thrown by an asynchronous javascript. To see this error much clearly, you can throw a response about the error to the web browser console, see below.

echo "<script>console.log(". $response .");</script>";

What the error is simply saying is that the asynchronous call made did not get any response from the PHP script it called.

Upvotes: 0

Mohammad Hamedani
Mohammad Hamedani

Reputation: 3354

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:

$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' ,

Upvotes: 2

Pawan Dongol
Pawan Dongol

Reputation: 1158

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>';

Upvotes: 0

Robin Choffardet
Robin Choffardet

Reputation: 460

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)

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.

By elimination $employee->Profile is the reasons why you script fails. It may is not set or not an object :)

Upvotes: 1

Related Questions