【发布时间】:2018-12-25 06:02:56
【问题描述】:
由于 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(),它会返回一个包含所需对象的数组。我只想根据关系检索对象。任何帮助表示赞赏。
【问题讨论】:
-
你怎么称呼
->schedule_block()? -
最后添加 ->toArray() 可能有助于将对象转换为数组
-
@Ijas schedule_block() 是从带有 with() Joshi 的控制器调用的 - 我试图获取对象,而不是使其成为数组
标签: laravel eloquent eloquent-relationship