5
My Product model has the following relationship:
public function categoria(){
return $this->belongsTo('estoque\Categoria');
}
and my Category model has:
public function produtos(){
return $this->hasMany('estoque\Produto');
}
When trying to access the view
:
<td> {{$p->categoria}} <td>
Call me back :
{"id":1,"nome":"Cerveja","descricao":"Todas cervejas","ativo":1,"created_at":"2015-10-24 13:53:14","updated_at":"2015-10-24 13:53:14"}
But I only need the category name, so I’m trying to access it like this:
<td> {{$p->categoria->nome}} <td>
But generates the following error:
Trying to get property of non-object
According to the documentation on the website of Lavael, I could use {{$p->categoria->nome}}
, but I can’t. Any idea?
Rafael, how is your model Categoria? The inverse ratio has been defined
hasOne
?– gmsantos
I edited the question.
– Rafael
Try to use as an array
<td> {{$p->categoria['nome']}} <td>
– HwapX
Yes this works, but I had seen some examples on the internet and they accessed as object and not array.
– Rafael
Quite strange Rafael, I couldn’t reproduce your problem here. Try updating with a
composer update
, eeu code is versioned somewhere?– gmsantos
It is not only on my pc, and I realized the
composer update
.– Rafael
I’ve seen that example somewhere ...
– rray
Do you have any product related to the category?
– rray
In this example a product belongs to a category, but I don’t know why it returns an array
– Rafael