I have problem with laravel , in my localhost everthing is fine but on my shared hosting i got problem even is the same php version
For example :
I want to get question title using $answer->question->title but i got error Trying to get property of non-object (View: /new/resources/views/users/show.blade.php)
But the project work fine in my localhost
Controller :
public function show($id)
{
$user = User::find($id);
$answers = AppAnswer::where('user_id','=',$user->id)
->with(['survey'])
->get();
$survey = AppSurvey::pluck('title', 'id')->toArray();
$question = AppQuestion::pluck('title', 'id')->toArray();
return view('users.show', compact('user','survey','answers','question'));
}
View blade :
<table class="table">
<thead class="thead-light">
<tr>
<th>{{ __('Question') }}</th>
<th>{{ __('Answers') }}</th>
<th>{{ __('Creation Date') }}</th>
</tr>
</thead>
<tbody>
@foreach($answers as $t)
<tr>
<td> {{ $t->question->id }} </td>
<td> {{ $t->answer }} </td>
<td> {{$t->created_at}} </td>
</tr>
@endforeach
</tbody>
</table>
In my localhost is working fine but in shared hosting i got :
ErrorException (E_ERROR)
Trying to get property of non-object (View: /new/resources/views/users/show.blade.php)
Previous exceptions
Trying to get property of non-object (0)
Please could you help me to fix that ?
3
Answers
your query returning array. If you dump it out, you might find that it’s an array and all you need is an array access
([])
instead of an object access(->).
Try calling it like this:
Hope that helps!
check if data in blade is array or object and check is array is parent object