以下是我的模型的关系:
音乐会
客户
field 属于音乐会(一对多)
时间段属于 field (一对多)
门票(多对多客户和时段)
我正在尝试获取时隙的多对多关系的计数,作为我的模型场所的作用域方法中的 where 子句的一部分。
public function scopeGetConcertVenuesThatHaveSlotsRemains($query, $concert_id)
{
$query->where('concert_id', $concert_id);
$query->whereHas('timeslots', function ($query2) {
$query2->where('slots', '>=', "Ticket.counts");
});
}
如何获取 Ticket.counts 值?
最佳答案
我自己得到了答案。
public function scopeGetConcertVenuesThatHaveSlotsRemains($query, $concert_id)
{
$query->where('concert_id', $concert_id);
$query->whereHas('timeslots', function ($query2) {
$query2->withCount('customers')->whereColumn('slots', '>', 'customers_count');
});
}
关于php - 范围方法的 where 子句中的 Laravel 数据透视表计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48899018/