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

时间:2017-10-13 20:39:12

标签: php laravel eloquent

我正在使用Laravel 5.4

工作代码

$cityWithEvents = City::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)]);
        }])->where('active', 1)->get()->keyBy('id');

不工作代码

$cityWithEvents = City::with('todayEventsWithAfterHoursIncluded')
            ->where('active', 1)
            ->get()
            ->keyBy('id');

城市模型

public function events() {
        return $this->hasManyThrough('App\Event', 'App\Venue');
    }

    public function todayEventsWithAfterHoursIncluded () {
        return $this->events()
            ->whereDate('start_time', Carbon::today('America/Montreal'))
            ->orwhereBetween('start_time', [
                Carbon::today('America/Montreal'),
                Carbon::tomorrow('America/Montreal')->addHours(4)
            ]);
    }

问题

尝试创建scope方法时,查询会给出不同的结果。我不明白为什么以及我应该改变什么

2 个答案:

答案 0 :(得分:1)

我只使用了scopes几次,但从未使用->with()条款。在City模型上,创建一个新的scope

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)]);
  });
}

然后,在City查询中,将其添加为scope函数:

$cityWithEvents = City->where('active', 1)
->todayEventsWithAfterHoursIncluded()
->get();

我认为您使用它的方式要求您的Event模型具有范围,因为您在技术上调用基本查询和范围内的with("events")

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

答案 1 :(得分:1)

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

$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)
             ]);
        }]);
}

现在它应该是平等的。

相关问题