如果子句不在Laravel中不返回正确的数据

时间:2019-06-30 05:49:54

标签: mysql laravel eloquent query-builder

假设我有口才查询

$drivers = Driver::whereRaw('driver_number not in (select driver_number from buses) OR driver_number = ?', [$bus->driver_number])
                    ->where('station_id', 3)
                    ->get();

所以我想获得分配给站点3的总线的所有驱动程序,但是它返回所有驱动程序,我只希望站点3的驱动程序,但这似乎不起作用。

这是驱动程序表

enter image description here

1 个答案:

答案 0 :(得分:0)

从总线上检索所有驾驶员编号

$driverNumbers = Bus::pluck('driver_number');

这将根据您上面提到的条件检索所有驱动程序。

$drivers = Driver::where('station_id', 3)
                 ->whereNotIn('driver_number', $driverNumbers)
                 ->where('driver_number', '<>', $bus->driver_number)
                 ->get();