Laravel查询使用分组依据和哪里不起作用

时间:2018-11-09 18:59:30

标签: laravel eloquent

这是我在原始sql中的查询。

Req::select('bldgs.name as building',DB::raw('count(location_id) as count_occupied'))   
    ->join('locations','locations.id','=','location_id')
    ->join('areas','areas.id','=','locations.area_id')
    ->join('floors','floors.id','=','areas.floor_id')
    ->join('bldgs as bl','bl.id','=','floors.bldg_id')
    ->where('reqs.status','=', '2') 
    ->where('start_date','<=', $date)
    ->where('end_date','>=', $date)
    ->groupBy('bldgs.name')

这是使它在Laravel中工作而不是原始工作的众多尝试之一。

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bldgs.name' in 'field list' (SQL: select `bldgs`.`name` as `building`, count(location_id) as count_occupied from `reqs` inner join `locations` on `locations`.`id` = `location_id` inner join `areas` on `areas`.`id` = `locations`.`area_id` inner join `floors` on `floors`.`id` = `areas`.`floor_id` inner join `bldgs` as `bl` on `bl`.`id` = `floors`.`bldg_id` where `reqs`.`status` = 2 and `start_date` <= 2018-10 and `end_date` >= 2018-10 group by `bldgs`.`name`)

我需要理解为什么第二种方法会导致mysql错误并拒绝运行上面的查询。是我的代码中有错误,还是使用eloguent这样的分组通常是不可能的,除了原始的mysql字符串之外?

这是我得到的错误。

Categorical

1 个答案:

答案 0 :(得分:0)

由于错误状态,您没有bldgs.name列。加入bldgsbl后,您将其命名。

将您的引用从bldgs.name重命名为bl.name

相关问题