Laravel 5.2 non-object problem in foreach

Asked

Viewed 140 times

0

I’m trying to pull the information from this relationship as you can see below:

Model:

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\views\13cbbd34b07174638fff7aab751ea42a1099d2cf.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\views\13cbbd34b07174638fff7aab751ea42a1099d2cf.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 presented whenever I try to access the relationship, it works if I use as array:

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

However this doesn’t help because I’m taking the id only as a test, actually I need to go deeper in this relationship:

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

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

  • If you use {{ $loc->enderero['logradouro']['nome'] }} won’t work?

  • enderero this correct?

  • Put the 3 models in your question...

1 answer

1

Opa, by what I noticed the question is being in Controller...vc is not calling the relations, just calling each Model separately...

In case you were supposed to call it that way..

//Controller.

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

vc ta informing which unit has to do with address and location...

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

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

ex:

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

Link: http://magazine.softerize.com.br/tutoriais/php/laravel/relacionamento-entre-tabelas-laravel

Browser other questions tagged

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