【问题标题】:Laravel Relationship problems: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\RelationLaravel 关系问题:关系方法必须返回 Illuminate\Database\Eloquent\Relations\Relation 类型的对象
【发布时间】:2018-12-15 16:22:21
【问题描述】:

我一定遗漏了一些明显的东西。我有一个 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

我在这里错过了什么?

【问题讨论】:

  • 顺便说一句,Invoice_detail::whereId(27)->first(); == Invoice_detail::find(27);
  • @JosephSilber 干净多了。谢谢!

标签: laravel laravel-4


【解决方案1】:

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

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

【讨论】:

  • 你无法想象我现在的耻辱。我花了一个多小时盯着它,重命名它,改变camelCasing,尝试下划线。这是史诗级别的掌心。谢谢。
猜你喜欢
  • 2018-03-20
  • 2014-07-09
  • 2014-06-26
  • 2017-12-21
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多