我有两个模型,比如
块和槽及其各自的表结构为:-
blocks(id, name);
slots(id, block_id, time, status);
slots.status
的可用值可能为0
或预订的值是1
。问题是,我只想返回那些
status
计数1
与插槽表中的总插槽计数比小于0.5的块,或者换句话说,我想返回那些插槽的可用度大于50%的预订。我有一个laravel雄辩的逻辑,如下:
$blocks = Block::whereHas('slots', function ($q) {
// What might be the condition here... ?
})
->with('slots');
最佳答案
我不确定我是否理解公式,但是如果您想获取有更多可用插槽的模块,则可以执行以下操作:
$blocks = Block::withCount(['slots as booked' => function($q) {
$q->where('status', 1);
}])
->withCount(['slots as available' => function($q) {
$q->where('status', 0);
}])
->get();
$blocks = $blocks->filter(function ($block) {
return $block->booked > $block->available;
});