我需要使用急切查询在 Laravel 中执行子查询,但结果始终为空。
查询:
$project = Log::where('project_id', $id)->with(['log_occurences'=> function($query){
$query
->selectRaw('date(created_at) as created, count(log_id) as count')
->groupBy('created');
}])
结果:
[
{
"id": 1,
"title": "Haskell\\Lorna\\Moore",
"level": "WARNING",
"stage": "production@wenders-macbook-pro",
"created_at": "2019-04-29 13:18:13",
"updated_at": "2019-05-06 20:24:32",
"log_occurences": [] <-- empty array
},
...
]
我很困惑,因为我尝试了更简单的查询,例如不使用聚合并且有效。
最佳答案
请记住,$query->selectRaw()
中需要主键(在本例中为 id)才能实际检索必要的结果。*
$project = Log::where('project_id', $id)->with(['log_occurences'=>function($query){
$query
->selectRaw('date(created_at) as created, count(log_id) as count,log_id')
->groupBy('created');
}])->get();
关于php - Laravel eager 查询返回一个空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56077969/