To get the correct events for a specific month, you can modify the query to check if the event starts or ends within the month. You can use the whereBetween method to check if the event starts or ends within the month. Here's an example:
$month = $request->input('month');
$year = $request->input('year');
$startOfMonth = Carbon::create($year, $month, 1)->startOfDay();
$endOfMonth = Carbon::create($year, $month, 1)->endOfMonth()->endOfDay();
return Event::whereHas('users', function ($query) use ($request) {
$query->where('user_id', $request->user()->id);
})
->where(function ($query) use ($startOfMonth, $endOfMonth) {
$query->whereBetween('init_date', [$startOfMonth, $endOfMonth])
->orWhereBetween('end_date', [$startOfMonth, $endOfMonth])
->orWhere(function ($query) use ($startOfMonth, $endOfMonth) {
$query->where('init_date', '<', $startOfMonth)
->where('end_date', '>', $endOfMonth);
});
})
->orderBy('init_date', 'asc')
->get();
In this example, we first create Carbon instances for the start and end of the month. We then modify the query to check if the event starts or ends within the month using the whereBetween method. We also check if the event spans across the month by using an orWhere clause with two where clauses to check if the event starts before the month and ends after the month.
Note that this assumes that the init_date and end_date columns are of type datetime or timestamp. If they are of type date, you will need to modify the Carbon instances accordingly.