【问题标题】:Laravel pivot table count in where clause of a scope method范围方法的 where 子句中的 Laravel 数据透视表计数
【发布时间】:2018-02-21 05:57:02
【问题描述】:

以下是我的模型的关系:

音乐会

客户

场地属于演唱会(一对多)

时隙属于场地(一对多)

门票(多对多客户和时间段)

我正在尝试获取时隙的多对多关系的计数,作为我的模型 Venue 的范围方法中 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 值?

【问题讨论】:

    标签: php mysql database laravel laravel-5


    【解决方案1】:

    我自己得到了答案。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2013-01-08
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多