1

We have a project upgraded from laravel v 5.1 to v 5.4 and many issues and bugs appear after upgrade anyway have this one

Trying to get property of non-object for index.blade.php

and this is the code

<tbody>
 @foreach($routeFEmails as $routeFEmail)

 <tr>
 <td>{{ $routeFEmail->id }}</td>
 <td>{{ $routeFEmail->routeF->id }} ({{ $routeFEmail->routeF->vessel_name }})</td>
 <td>{{ $routeFEmail->creator->type }}:&nbsp;{{ $routeFEmail->creator->first_name }} {{ $routeFEmail->creator->last_name }}</td>
 <td>{{ $routeFEmail->status }}</td>
 <td>{{ $routeFEmail->created_at->format('Y-m-d H:i') }}</td>
  </tr>
  @endforeach
 </tbody>

i check model and no value from what mention is null no NULL

i did also

php artisan cache:clear 
php artisan route:clear 
php artisan view:clear 

and this is a function in co controller

public function index()
{
    $routeFEmails = RouteFEmail::orderBy('id', 'desc')->paginate(10);
    return view('backend.route_f_emails.index', compact('routeFEmails'));
}

HOW I CAN FIX THIS ? :(

flower
  • 989
  • 4
  • 16
  • 34

2 Answers2

0

Your problem is with compact(), Return type of compact() is array [Read Here], so when you are passing compact('routeFEmails') it is converting object to array.

In index.blade.php you are accessing values of $routeFEmails as object. So you can try one of below solutions.

Solution 1

Write <td>{{ $routeFEmail['id'] }}</td> instead of <td>{{ $routeFEmail->id }}</td>

OR

Solution 2

Write

return view('backend.route_f_emails.index', ['routeFEmails'=>$routeFEmails]);

instead of

return view('backend.route_f_emails.index', compact('routeFEmails'));
Zedex7
  • 1,624
  • 1
  • 12
  • 21
0

trying to get a property on a non object means that the $routeFEmail is not an object not that some of it's properties like id is null. Also the part

$routeFEmail->routeF

could be null ( My guess that the problem is there) you can skip it with

@if( !is_null($routeFEmail->routeF) )
write something 
@endif

or even dd if it is null

Maky
  • 521
  • 5
  • 21