Laravel Relationship问题:关系方法必须返回Illuminate \ Database \ Eloquent \ Relations \ Relation类型的对象

时间:2014-03-04 00:38:34

标签: laravel laravel-4

我必须遗漏一些明显的东西。我有一个Invoice_detail模型:

class Invoice_detail extends Eloquent {

public function products()
{
    $this->belongsTo('Product');

}
}

产品型号:

class Product extends Eloquent {

public function invoiceDetails()
{
    $this->hasMany('Invoice_detail');
}
}

但是当我尝试使用它时:

Route::get('/', function(){

  $detail =  Invoice_detail::whereId(27)->first();

  return $detail->products;
});

我得到:Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

是的 - 你的关系方法应该有回报:

public function invoiceDetails()
{
    return $this->hasMany('Invoice_detail');
}