Problem in a foreach

Asked

Viewed 315 times

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?

  • I believe the error is actually in the next line ({{$user->empresa->razao_social}})...because it is impossible, unless you have overwritten the method all(), you have that information that way. You would have to do a join or another separate query in the company table to have this information.

  • @jbueno, this is the magic of Laravel, everything he passes in the second parameter of the method view() is "transformed" into variables in 'Blade', which is an evolved 'smart'...

  • Ah, yes. Thank you @Kennyrafael. I thought some keyword (Model .$data, for example) to access this.

  • @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 ^^

  • ah understood...at least I hit the line...hahahaha

  • @Kennyrafael haha good :)

Show 2 more comments

2 answers

4

I know you’ve already posted an answer, but another legal solution in the Laravel would be using the or in the desired expression:

{!! $user->empresa->razao_social or "Sem empresa" !!}
  • I liked the solution, but the way I used I can use a css class or html tag, even writing a little more code ^^

  • @Felipepaetzold was just an add-on. It’s useful to do as I did in table cases. Then you can put a value when the expression or evaluate the first sentence as invalid

1


I solved the problem, some users are without company, so the company method that has a belongs_to with user sometimes returns empty, so does not come the properties of empresa.

I solved with the following parole in view:

@if($user->empresa)
    <td class="text-center"><a href="/empresas/detalhes/{{$user->empresa->id}}">{{$user->empresa->razao_social}}</a></td>
@else
    <td class="text-center">
        <strong>Sem empresa</strong>
    </td>
@endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.