Php 拉维5中的关系

Php 拉维5中的关系,php,laravel-5,Php,Laravel 5,我的产品型号: public function categoria(){ return $this->belongsTo('estoque\Categoria'); } 我的模型Categoria有: public function produtos(){ return $this->hasMany('estoque\Produto'); } 在视图视图中尝试从produto访问: <td> {{$p->categoria}} <t

我的产品型号:

public function categoria(){
    return $this->belongsTo('estoque\Categoria');
}
我的模型Categoria有:

public function produtos(){
        return $this->hasMany('estoque\Produto');
}
在视图
视图中尝试从produto访问:

<td> {{$p->categoria}} <td>
这不应该返回Categoria的对象? 我想这样访问:

<td> {{$p->categoria->nome}} <td>
return $this->hasMany('estoque\Produto', 'foreign_key', 'local_key');
但它是一个数组:

{"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"}
Trying to get property of non-object

但在文档中,返回的是对象而不是数组。有什么想法吗?

您可能需要在关系中指定本地键和外键。 选中此项:

你忘了写这样的东西:

<td> {{$p->categoria->nome}} <td>
return $this->hasMany('estoque\Produto', 'foreign_key', 'local_key');
如果不使用约定的ID名称,则应指定它。在这种情况下

return $this->hasMany('estoque\Produto', 'categoria_id', 'id');
return $this->belongsTo('estoque\Categoria', 'id', 'categoria_id');

在装载产品时,您是否尝试过使用渴望装载?尝试
$p=Product::with('categoria')->get()
返回总是一个对象。您的类别id字段在产品模型上是如何命名的?@LuisDalmolin categoria_id您不是偶然使用了
toArray()
吗?只是
返回$this->belongsTo('estoque\categoria',categoria_id')工作,编辑您的答案。谢谢你,伙计。