I'm creating calendar for few days events and the events can not overlap. But event can start/end when another event starts/ends.
In Laravel, in my event model table I am storing start and end date of events. How can I check before storing new event do database, if it is valid (is not overlapping with existing events, excluding margin/boundary days) ?
In advance thank You very much for Your help.
You can use following code to get the correct counts for events booked between certain times:
$eventsCount = Events::where(function ($query) use ($startTime, $endTime) {
$query->where(function ($query) use ($startTime, $endTime) {
$query->where('start', '>=', $startTime)
->where('end', '<', $startTime);
})
->orWhere(function ($query) use ($startTime, $endTime) {
$query->where('start', '<', $endTime)
->where('end', '>=', $endTime);
});
})->count();
Use it for all variants intersections date ranges. It help for me
$rangeCount = TransferSchedule::where(function ($query) use ($from, $to) {
$query->where(function ($query) use ($from, $to) {
$query->where('date_from', '<=', $from)
->where('date_to', '>=', $from);
})->orWhere(function ($query) use ($from, $to) {
$query->where('date_from', '<=', $to)
->where('date_to', '>=', $to);
})->orWhere(function ($query) use ($from, $to) {
$query->where('date_from', '>=', $from)
->where('date_to', '<=', $to);
});
})->count();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With