按关系属于所有领域订购集合

时间:2018-03-19 07:13:26

标签: laravel laravel-5 laravel-5.5 laravel-eloquent laravel-orm

我拥有属于日期模型的事件模型

public function dates () {
    return $this->belongsToMany('App\Date', 'event_date');
}

每个日期都有开始日期和结束日期。

居:

事件 - > name = StackOverflow会议

事件 - > dates()=

[0](' start' => 01-24-2018,' end' => 01-27-2018)

[1](' start' => 04-24-2018,' end' => 04-27-2018)

活动 - >名称=复活节派对

事件 - > dates()=

[0](' start' => 04-18-2018,' end' => 04-19-2018)

我想根据今天的日期按开始日期订购活动, 所以在示例"复活节派对"是第一个事件,因为第一个开始日期>今天约会。然后:

(' Easter Party',' StackOverflow Meeting')

如何设法创建此类订单?

1 个答案:

答案 0 :(得分:2)

如果您与event模型之间存在Date关系,我认为您可以这样做

// Get dates with event, sort by start date 
$datesWithEvents = Date::whereBetween($yesterday,$today)->orderBy('start')->with('event');

// you can Extract only events
$event = $datesWithEvents->pluck('event'); // I think you need to add "->with('event')" into the return event method on Date Model
// or directly return dates with events
return $datesWithEvents;