简体   繁体   English

如何存储一个事件的多个日期范围?

[英]How to store multiple date ranges of a event?

Basically i am creating an application where one event can have multiple date ranges like below and i want to show these events on jQuery calendar. 基本上我正在创建一个应用程序,其中一个事件可以具有如下所示的多个日期范围,我想在jQuery日历上显示这些事件。

Event Table ID Name 1 Test Event

Event Date Table ID Event_ID Start Date End Date 1 1 2018-04-22 2018-04-22 2 1 2018-04-25 2018-04-27 3 1 2018-05-01 2018-05-10

There is Event model but Event date doesn't have any model So how can i do this with laravel eloquent relationship? 有事件模型,但事件日期没有任何模型,那么我该如何与Laravel雄辩的关系做到这一点?

And how do i check particular event is available in specific date range while retrieving events from database. 从数据库检索事件时,如何检查特定事件在特定日期范围内可用。

Thanks in advance! 提前致谢!

You can make a model for "Event Date Table" and then reference it in event model with has many relation. 您可以为“事件日期表”创建一个模型,然后在具有很多关系的事件模型中引用它。 first of all, you must make a model in command prompt with this line: 首先,您必须在命令提示符中使用以下行创建模型:

php artisan make:model EventDate

and then you must set $table property in EvebtDate model: 然后必须在EvebtDate模型中设置$ table属性:

protected $table='Event Date Table';

and belong to relation to event model: 并属于与事件模型的关系:

    public function event(){
       return $this->belongsTo(Event::class,'Event_ID' );
    }

after that in your Event model you must set hasmany relation to EventDate model: 之后,您必须在Event模型中设置hasMany与EventDate模型的关系:

    public function eventDates(){
        return $this->hasMany(EventDate::class,'Event_ID' );
    }

after that you can retrieve all records from Event Date table from Event with that relationship. 之后,您可以从具有该关系的事件的事件日期表中检索所有记录。 if you use carbon to insert data to date colums you can do somthing like this: 如果您使用Carbon插入数据以更新日期列,则可以执行以下操作:

$eventDates = EventDate::whereBetween('Start Date', [$dateStart->format('Y-m-d'), $dateE->format('Y-m-d')])
->whereBetween('End Date', [$dateStart->format('Y-m-d'), $dateE->format('Y-m-d')])
->get();

then you must do a foreach loop to get the events and put them into an array: 那么您必须执行一个foreach循环来获取事件并将它们放入数组中:

$events = [];

foreach($eventDates as $date){
  $events[] = eventDates->event()->first();
}

this way you can retrieve all the events available in a specific date range. 这样,您可以检索特定日期范围内的所有可用事件。

Hope this would help! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM