如何获得介于日期之间的Rows,并将他们的孩子Rows放在另一张表中-Laravel Eloquent



我想创建一个查询,它将获取日期之间的所有Event。我也想得到他们所有的孩子EventImage

我能把所有这些都放到一个查询中吗?

事件模型

public function eventImages()

{

return $this->hasMany(EventImage::class, 'event_id');

}

事件图像模型

public function event()

{

return $this->belongsTo(Event::class);

}

获取日期之间事件的示例查询

$events = Event::whereBetween('date', [$start_date, $end_date])
->orderBy('date', 'ASC')
->get();

谢谢!

您应该已经能够访问它们了。为了确保它们包含在序列化中,您可以通过with()加载它们。

Event::with(['eventImages'])
->whereBetween('date', [$start_date, $end_date])
->orderBy('date', 'ASC')
->get();

相关内容

最新更新