This is my View
:
<tbody>
@if(isset($pessoas))
@foreach($pessoas as $p)
<tr>
<td>{{$p->nome}}</td>
<td>{{$p->idade}}</td>
<td>{{$p->cidade->nome}}</td>
@endforeach
@endif
</tbody>
The% w /% passing the people parameter and returns Controller
:
public function verPessoas()
{
$pessoas = Pessoa::all();
return view('verPessoas', compact('pessoas'));
}
Link in View
Model
public function cidade()
{
return $this->belongsTo('App\Cidade');
}
Relationship in Model "City"
public function pessoa()
{
return $this->hasMany('App\Pessoa');
}
This relationship is 1: N , where the Pessoa
table receives the foreign key of pessoas
( cidade
), being a city has several people, each person belongs to a city . When I try to access this cidade_id
I get error :
View
When I remove the line
<td>{{$p->cidade->nome}}</td>
Everything works normally, leading me to believe that I am relating Trying to get property of non-object (View: C:\xampp\htdocs\projetos\PROJETO\resources\views\verPessoas.blade.php)
in the wrong way and checked the bank tables and their values, everything is filled and working perfectly.
I would like to understand what causes this error and what is the appropriate solution.