Php 雄辩范围法给出了不同的结果

Php 雄辩范围法给出了不同的结果,php,laravel,eloquent,Php,Laravel,Eloquent,我使用的是Laravel 5.4 工作代码 非工作代码 城市模型 问题 当尝试创建范围方法时,查询会给出不同的结果。我看不出我为什么要更改,应该更改什么我只使用了作用域几次,但从未在->with()子句中使用过。在您的城市模型上,创建一个新的范围: public function scopeTodayEventsWithAfterHoursIncluded($query){ return $query->with(["events" => function($subQuery){

我使用的是
Laravel 5.4

工作代码 非工作代码 城市模型 问题
当尝试创建
范围
方法时,查询会给出不同的结果。我看不出我为什么要更改,应该更改什么

我只使用了
作用域
几次,但从未在
->with()子句中使用过。在您的
城市
模型上,创建一个新的
范围

public function scopeTodayEventsWithAfterHoursIncluded($query){
  return $query->with(["events" => function($subQuery){
    $subQuery->whereDate('start_time', Carbon::today('America/Montreal'))->orWhereBetween('start_time', [Carbon::today('America/Montreal'), Carbon::tomorrow('America/Montreal')->addHours(4)]);
  });
}
然后,在您的
城市
查询中,将其添加为
范围
函数:

$cityWithEvents = City->where('active', 1)
->todayEventsWithAfterHoursIncluded()
->get();
我认为您使用它的方式要求您的
事件
模型具有作用域,因为从技术上讲,您在基本查询和作用域查询上使用(“事件”)
调用


如果这会改变您的结果,请告诉我。

我只使用了
范围
几次,但从未在
->with()子句中使用过。在您的
城市
模型上,创建一个新的
范围

public function scopeTodayEventsWithAfterHoursIncluded($query){
  return $query->with(["events" => function($subQuery){
    $subQuery->whereDate('start_time', Carbon::today('America/Montreal'))->orWhereBetween('start_time', [Carbon::today('America/Montreal'), Carbon::tomorrow('America/Montreal')->addHours(4)]);
  });
}
然后,在您的
城市
查询中,将其添加为
范围
函数:

$cityWithEvents = City->where('active', 1)
->todayEventsWithAfterHoursIncluded()
->get();
我认为您使用它的方式要求您的
事件
模型具有作用域,因为从技术上讲,您在基本查询和作用域查询上使用(“事件”)
调用


如果这会改变您的结果,请告诉我。

如果您进行查询,您应该这样做:

$cityWithEvents = City::withTodayEventsWithAfterHoursIncluded()
            ->where('active', 1)
            ->get()
            ->keyBy('id');
public function scopeWithTodayEventsWithAfterHoursIncluded ($query)
{
    return $query
        ->with(['events' => function ($q) {$q
            ->whereDate('start_time', Carbon::today('America/Montreal'))
            ->orwhereBetween('start_time', [
                Carbon::today('America/Montreal'),
                Carbon::tomorrow('America/Montreal')->addHours(4)
             ]);
        }]);
}
模型中的范围应如下所示:

$cityWithEvents = City::withTodayEventsWithAfterHoursIncluded()
            ->where('active', 1)
            ->get()
            ->keyBy('id');
public function scopeWithTodayEventsWithAfterHoursIncluded ($query)
{
    return $query
        ->with(['events' => function ($q) {$q
            ->whereDate('start_time', Carbon::today('America/Montreal'))
            ->orwhereBetween('start_time', [
                Carbon::today('America/Montreal'),
                Carbon::tomorrow('America/Montreal')->addHours(4)
             ]);
        }]);
}

现在应该是相等的。

如果执行查询,则应如下所示:

$cityWithEvents = City::withTodayEventsWithAfterHoursIncluded()
            ->where('active', 1)
            ->get()
            ->keyBy('id');
public function scopeWithTodayEventsWithAfterHoursIncluded ($query)
{
    return $query
        ->with(['events' => function ($q) {$q
            ->whereDate('start_time', Carbon::today('America/Montreal'))
            ->orwhereBetween('start_time', [
                Carbon::today('America/Montreal'),
                Carbon::tomorrow('America/Montreal')->addHours(4)
             ]);
        }]);
}
模型中的范围应如下所示:

$cityWithEvents = City::withTodayEventsWithAfterHoursIncluded()
            ->where('active', 1)
            ->get()
            ->keyBy('id');
public function scopeWithTodayEventsWithAfterHoursIncluded ($query)
{
    return $query
        ->with(['events' => function ($q) {$q
            ->whereDate('start_time', Carbon::today('America/Montreal'))
            ->orwhereBetween('start_time', [
                Carbon::today('America/Montreal'),
                Carbon::tomorrow('America/Montreal')->addHours(4)
             ]);
        }]);
}
现在应该是平等的