首页 > 解决方案 > 使用 hasMany 关系进行雄辩的急切加载不起作用

问题描述

我的代码目前不起作用,但我的关系是正确的。

项目模型

public function timeEntries()
{
    return $this->hasMany('App\Model\TimeEntries');
}

$array_of_project = Project::with('timeEntries')
    ->whereBetween('spent_on', [($request->debut)), ($request->fin))])
    ->get();

它没有找到“spent_on”。

标签: phplaraveleloquent

解决方案


看起来您正在尝试限制关系的记录。我建议查看有关限制急切加载的文档。这是我认为您正在尝试完成的代码示例:

$projects = App\Model\Project::with(['timeEntries' => function ($query) use ($request) {
    $query->whereBetween('spent_on', $request->debut, $request->fin);
}])->get();

https://laravel.com/docs/5.8/eloquent-relationships#constraining-eager-loads


推荐阅读