【发布时间】:2017-11-15 10:22:17
【问题描述】:
是否可以创建一个快速的方法来从一对多关系中返回第一个模型?这是我的代码,来自模型文件:
public function books() {
return $this->hasMany('App\Models\Book');
}
public function first_book() {
return $this->book()->first();
}
这是我得到的错误:
Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints()
之所以要使用这个,是为了可以使用with()方法收集第一条记录,例如:
$authors = Author::with('first_book')->select('*');
我将这些记录与数据表一起使用。
【问题讨论】: