简体   繁体   English

Laravel数据透视表在范围方法的where子句中的计数

[英]Laravel pivot table count in where clause of a scope method

Following are my model's relationship: 以下是我的模型的关系:

Concerts 演唱会

Customers 顾客

Venues belongs to concert (One to Many) 场地属于音乐会(一对多)

Timeslots belongs to venue (One to Many) 时隙属于场所(一对多)

Tickets (Many to Many of Customers & Timeslots) 门票(许多顾客和时段很多)

I'm trying to get the count of a many to many relationship of timeslot as part of a where clause in a scope method of my model Venue. 我正在尝试将时隙的多对多关系作为我的模型Venue范围方法中where子句的一部分进行计数。

public function scopeGetConcertVenuesThatHaveSlotsRemains($query, $concert_id)
{
    $query->where('concert_id', $concert_id);
    $query->whereHas('timeslots', function ($query2) {
        $query2->where('slots', '>=', "Ticket.counts"); 
    });
}

How do I get the Ticket.counts value? 我如何获得Ticket.counts值?

Got the answer myself. 我自己得到了答案。

public function scopeGetConcertVenuesThatHaveSlotsRemains($query, $concert_id)
{
    $query->where('concert_id', $concert_id);
    $query->whereHas('timeslots', function ($query2) {
        $query2->withCount('customers')->whereColumn('slots', '>', 'customers_count'); 
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM