【问题标题】:How can I implement Laravel Eloquent wherehas count question?如何实现 Laravel Eloquent wherehas count question?
【发布时间】:2019-07-08 00:15:44
【问题描述】:

我有预订和插件表。

bookings 有很多插件。

我想获得带有插件类型可选旅游的预订

我试过了

 $bookings = Booking::with([
        "add_ons",  
         ])
        ->whereHas("add_ons",function($q){
            $q->where("type","<>" ,AddOn::T_OPTIONAL_TOUR);
        })

这显然不起作用。 我怎样才能只获得没有可选旅游插件的预订?

这是表格信息

预订

id
name

Booking_add_ons

id
booking_id
add_on_id

插件

id
type
add_name

【问题讨论】:

  • 好吧,我认为我的问题并不清楚。
  • 我需要在插件没有可选旅行的情况下使用插件进行预订。

标签: laravel eloquent wherehas


【解决方案1】:

使用whereDoesntHave():

$bookings = Booking::with('add_ons')
    ->whereDoesntHave('add_ons', function($q) {
        $q->where('type', '=', AddOn::T_OPTIONAL_TOUR);
    })->get();

【讨论】:

  • 谢谢。这行得通。我有更多导致故障的 where 子句。
猜你喜欢
  • 2021-08-23
  • 1970-01-01
  • 2019-12-28
  • 2017-06-09
  • 2020-09-16
  • 2014-09-30
  • 2020-12-23
  • 2021-05-25
  • 2018-06-19
相关资源
最近更新 更多