Laravel DB::raw

Laravel DB::raw

   Trip::select('trips.id','trips.date_trip',
        DB::raw('(select count(region_id) as count from trip_regions where trip_id=trips.id) as count')
    )->where('count',10)->get();

错误

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 
'where clause' (SQL: select `trips`.`id`, `trips`.`date_trip`, (select 
count(region_id) as sum from trip_regions where trip_id=trips.id) as 
count from `trips` where `count` = 10)

我有查询行想要DB::raw。你能帮助我吗???非常感谢

尝试使用 having 子句

Trip::select('trips.id','trips.date_trip',
    DB::raw('(select count(region_id) as count from trip_regions where trip_id=trips.id) as count')
)->having('count', '=',10)->get();

您不能对自定义别名应用 where 子句,where 仅适用于您的 table(s) 中存在的列。要过滤掉 expressions/aggregate 结果集的结果,您需要 having 子句