由于hasMany的关系,我试图返回来自集合数组的对象的响应。
我试图退货$block->where('date','=',$today)->first();
错误说:调用未定义的方法 App \ BlockDate :: addEagerConstraints()
public function block_dates()
{
return $this->hasMany(BlockDate::class);
}
public function schedule_block()
{
$today = Carbon::today()->toDateString();
$block = $this->block_dates();
return $block->where('date','=',$today)->first();
}
schedule_block()
应该返回BlockDate
的对象。如果删除first()
,它将返回一个包含所需对象的数组。我只想根据该关系检索该对象。任何帮助表示赞赏。
答案 0 :(得分:2)
尝试这个:
public function schedule_block() {
$today = Carbon::today()->toDateString();
return $this->hasOne(BlockDate::class)->where('date','=',$today);
}