Laravel渴望查询返回一个空列表

时间:2019-05-10 12:42:19

标签: php mysql laravel eloquent

我需要使用渴望的查询对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
 },
 ...
]

我很困惑,因为我尝试了更简单的查询,例如不使用聚合并且可以正常工作。

1 个答案:

答案 0 :(得分:0)

请记住,$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();