Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.3多条件连接有计数的雄辩连接表_Php_Laravel_Join_Eloquent_Having Clause - Fatal编程技术网

Php Laravel 5.3多条件连接有计数的雄辩连接表

Php Laravel 5.3多条件连接有计数的雄辩连接表,php,laravel,join,eloquent,having-clause,Php,Laravel,Join,Eloquent,Having Clause,我对使用having count选择要显示的主表的左联接表有疑问,条件如下: select b.id, b.location_name, b.box_identity from box b left join device d on b.id = d.box_id and d.deleted_at is null group by b.id, b.location_name, b.box_identity having count(d.

我对使用having count选择要显示的主表的左联接表有疑问,条件如下:

select  b.id, b.location_name, b.box_identity
from    box b
    left join device d on b.id = d.box_id
                      and d.deleted_at is null
group by b.id, b.location_name, b.box_identity
having count(d.*) < COALESCE(b.device_slot, 3)
order by b.id desc
选择b.id、b.location\u name、b.box\u identity
从方框b
b.id=d.box\U id上的左连接设备d
d.deleted_at为空
按b.id、b.location\u name、b.box\u标识分组
具有计数(d*)<合并(b.设备\u插槽,3)
按b.id描述订购
我尝试过使用像这样雄辩的代码,但DB::raw在$join函数中不起作用

$boxes = Box::leftJoin('device', function($join) {
    $join->on('box.id', '=', 'device.box_id');
    $join->on(DB::raw('device.deleted_at is null'));
})
->select('box.id', 'box.box_identity', 'box.location_name')
->groupBy('box.id', 'box.box_identity', 'box.location_name')
->havingRaw('COUNT(device.*) < COALESCE(box.device_slot, 3)')
->orderBy('box.id', 'desc')
->get();
$Box=Box::leftJoin('device',function($join){
$join->on('box.id','=','device.box_id');
$join->on(DB::raw('device.deleted_at为null');
})
->选择('box.id'、'box.box\u identity'、'box.location\u name')
->groupBy('box.id'、'box.box\u identity'、'box.location\u name')
->havingRaw('COUNT(device.*)<合并(box.device_插槽,3)'
->orderBy('box.id','desc')
->get();
如何使用laravel eloquent实现此查询?提前谢谢

使用以下命令:

$boxes = Box::leftJoin('device', function($join) {
    $join->on('box.id', '=', 'device.box_id');
    $join->whereRaw('device.deleted_at is null');
})

您可以尝试
$join->whereRaw('device.deleted_at为null')on('device.deleted_at','=',DB::raw('NULL');但结果仍然是无效的。安装了3个设备的框仍然显示,必须不显示。try DB::enableQueryLog();dd(DB::getQueryLog());最后。然后在此处发布查询您的查询将检查MySQL中的
deleted\u at=“NULL”
字符串,而不是
deleted\u at为NULL
。在这种情况下,必须使用
原始查询
@BRjava是的,现在这是正确的,就像海当的答案一样