Problem Laravel 5.2 non-object in foreach

0

I'm trying to pull the information out of this relationship as you can see below:

Template:

public function endereco(){
    return $this->belongsTo('App\Models\End\Logradouro_Bairro');
}

public function local(){
    return $this->hasMany('App\Models\Admin\Local');
}

Controller:

public function indexLocal()
 {
    $local = Local::all();
    $secretaria = UnidadeOrganizacional::all();
    $endereco = Logradouro_Bairro::all();
    return view('admin.local')->with('local',$local)->with('secretaria',$secretaria)->with('endereco',$endereco);
 }

View

@foreach($endereco as $loc)
                          <tr data-id="{{ $loc->id }}" data-sec="{{ $loc->secretaria_id }}">
                            <td>{{ $loc->secretaria->sigla }}</td>
                            <td>{{ $loc->nomeDoLocal }}</td>
                            <td>{{ $loc->endereco->id }}</td>
                            <td>{{ $loc->telefone }}</td>
                            <td>{{ $loc->telefone2 }}</td>
                            <td>{{ $loc->telefone3 }}</td>
                            <td>{{ $loc->telefone4 }}</td>
                            <td><i class="material-icons editar md-color-cyan-700" style="font-size:16px; cursor:pointer;" data-toggle="modal" data-target="#myModal5">&#xE150;</i></td>
                            <td><i class="deletar fa fa-times md-color-red-400" style="font-size:16px; cursor:pointer;" data-toggle="modal" data-target="#myModal6"></i></td>
                          </tr>
                        @endforeach

Error:

Trying to get property of non-object in 13cbbd34b07174638fff7aab751ea42a1099d2cf.php line 121
at HandleExceptions->handleError('8', 'Trying to get property of non-object', 'C:\laragon\www\feed\storage\framework\viewscbbd34b07174638fff7aab751ea42a1099d2cf.php', '121', array('__path' => 'C:\laragon\www\feed\storage\framework\views/13cbbd34b07174638fff7aab751ea42a1099d2cf.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'local' => object(Collection), 'secretaria' => object(Collection), 'endereco' => object(Collection)), 'obLevel' => '1', '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'local' => object(Collection), 'secretaria' => object(Collection), 'endereco' => object(Collection), 'sec' => object(UnidadeOrganizacional), 'end' => object(Logradouro_Bairro), 'loc' => object(Logradouro_Bairro))) in 13cbbd34b07174638fff7aab751ea42a1099d2cf.php line 121
at include('C:\laragon\www\feed\storage\framework\viewscbbd34b07174638fff7aab751ea42a1099d2cf.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('C:\laragon\www\feed\storage\framework\views/13cbbd34b07174638fff7aab751ea42a1099d2cf.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'local' => object(Collection), 'secretaria' => object(Collection), 'endereco' => object(Collection))) in CompilerEngine.php line 59
at CompilerEngine->get('C:\laragon\www\feed\resources\views/admin/local.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'local' => object(Collection), 'secretaria' => object(Collection), 'endereco' => object(Collection))) in View.php line 149

This error is displayed whenever I try to access the relationship, it works if I use it as an array:

{{ $loc->enderero['id'] }}

But this does not help because I'm just taking the id as a test, in fact I need to go deeper in this relationship:

{{ $loc->endereco->logradouro->nome }}

Has anyone ever had this kind of problem? Could you help me?

    
asked by anonymous 06.01.2017 / 12:16

1 answer

1

Oops, so I noticed the issue being in the Controller ... you're not calling the relationships, just calling each Model separately ...

In case it was for you to call it that way ..

// Controller.

$ secretariat = Organizational Unit :: with ('address', 'local') - > get ();

Are you reporting that drive is related to address and location ...

See the link below that you will understand better ...

Ps. If the relationship attributes do not have Laravel defaults you will have to enter the relationship keys in the (local and address) methods;

ex:

public function endereco(){
  return $this->hasOne('App\Models\Admin\Local', 'foreign_key', 'local_key');
}

Link: link

    
25.01.2017 / 23:43