3
I’m using Laravel 5.3, and by making a foreach
in the view, it does not recognize as an object, but if I print only what is in the variable $user
it prints an object json
.
Controller:
public function getAll()
{
return view('user.index',
[
'data' => $this->user->all(),
'nav' => $this->nav
]
);
}
Blade:
@foreach($data as $user)
<tr>
<td class="text-center">{{$user->id}}</td>
<td class="text-center"><a href="/users/detalhes/{{$user->id}}">{{$user->name}}</a></td>
<td class="text-center">{{$user->email}}</td> //essa é a linha que acusa o erro
<td class="text-center"><a href="/empresas/detalhes/{{$user->empresa->id}}">{{$user->empresa->razao_social}}</a></td>
</tr>
@endforeach
Error:
Errorexception in bd796ee4ce7ef1a15a679338ab7787ec13bf0e7d.php line 34: Trying to get Property of non-object (View: C: dev comercio controlnet Resources views user index.blade.php)
Someone managed to find the mistake?
I don’t program PHP, I don’t know Laravel, but it makes sense to use only
$data
in view? If you print$data
what is the result?– Jéf Bueno
I believe the error is actually in the next line (
{{$user->empresa->razao_social}}
)...because it is impossible, unless you have overwritten the methodall()
, you have that information that way. You would have to do ajoin
or another separate query in the company table to have this information.– Kenny Rafael
@jbueno, this is the magic of
Laravel
, everything he passes in the second parameter of the methodview()
is "transformed" into variables in 'Blade', which is an evolved 'smart'...– Kenny Rafael
Ah, yes. Thank you @Kennyrafael. I thought some keyword (
Model
.$data
, for example) to access this.– Jéf Bueno
@Kennyrafael discovered the problem kkkk, is that some records come without company, so the Aravel tries to catch an object that does not exist, but works yes, I’m using company as a belongs_to method, but it was worth the help ^^
– Felipe Paetzold
ah understood...at least I hit the line...hahahaha
– Kenny Rafael
@Kennyrafael haha good :)
– Felipe Paetzold