Trying to get Property of non-object?

Asked

Viewed 2,424 times

1

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 Controller passing the parameter people and returns to View:

public function verPessoas()
{      
    $pessoas = Pessoa::all();
    return view('verPessoas', compact('pessoas'));
}

Relationship in Model Pessoa

public function cidade()
{
    return $this->belongsTo('App\Cidade');
}

Relation in the Model "City"

public function pessoa()
{
    return $this->hasMany('App\Pessoa');
}

This relationship is 1:N, where the table pessoas receives the foreign key from cidade (cidade_id), being a city has several people, each person belongs to a city. When I try to access this View get the error:

Trying to get property of non-object (View: C:\xampp\htdocs\projetos\PROJETO\resources\views\verPessoas.blade.php)

When I remove the line

<td>{{$p->cidade->nome}}</td>

Everything works normally, leading to believe that I am relating the models wrong way and checked the tables of the bank and their respective values, everything is filled and working perfectly.

I would like to understand what causes this error and what the appropriate solution.

2 answers

2

In your relationship with the other model put the attribute that makes the link (foreign key) as the second parameter, it can be this since the eloquent need to know which parameter to use in the query if your foreign key name is not in the default (tableName)_id.

In person:

public function cidade(){
    return $this->belongsTo(Cidade::class, 'cidade_id');
}

In Cidade:

public function pessoa(){
    return $this->hasMany(Pessoa::class, 'cidade_id');
}
  • 1

    Possibly this is the problem, just remembering that the definition FK is only needed if it is not tabela_id, what may be what is happening.

  • @Pliavi yes, I will complement in the answer.

  • my foreign key has the default name. I have tested the above suggestion but not solved it yet

  • @Marcelosnts can show you how his $fillable ?

  • @Felipe Paetzold Pessoa: protected $fillable = array('nome', 'idade'); City: protected $fillable = array('nome');

  • @Marcelosnts Put city_id in person fillable

  • put, but still not working, continues the same error.

  • @Marcelosnts strange, return this way to the view then return view('verPessoas', ['pessoas' => $pessoas]);, if it doesn’t work you can try to manipulate city as array <td>{{$p->cidade['nome']}}</td> but apparently it’s all right

  • also did not work, the first suggestion showed the same error and the second left a blank field, however, displayed the other information in the same way when removing the <td>{{$p->cidade->nome}}</td>.

  • @Marcelosnts In the controller do a foreach of $pessoas and inside the loop use dd($pessoa->cidade) and see what returns

  • @Felipe Paetzold, sorry, I ended up not testing this way because the solution proposed by Virgilio Novic solved my problem. I appreciate the help.

Show 6 more comments

0


If your relationships are correct, do so:

With with:

public function verPessoas()
{      
    $pessoas = Pessoa::with('cidade')->get()
    return view('verPessoas', compact('pessoas'));
}

One reason this is the recommended way: in that case two SQL and the logic of the algorithm makes the inclusion of the items in its destination that has a great differential in the performance of the application, since the other way to each interaction of the for is made a new SQL greatly degrading the application

References:

  • 1

    worked perfectly that way, I appreciate the help and also for explaining why this would be a recommended way.

Browser other questions tagged

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