I have an application in laravel, which manages the holidays, I have a problem when filtering the dates selected by the workers, since I am only able to filter the start and end date, the problem is that according to the department 2 workers can not take vacations on the same date, but if I select for example from 1 to 15 with worker 1 it should be impossible for worker 2 to select for example from 2 to 14, however as I am only able to filter the date Start and end I accept it, and save with which the filter does not work.
My question is if there is not a way to filter a range of dates, instead of start and end dates. I leave the code:
public function store(Request $request)
{
$this->validate($request, [
'type' => 'required',
'observations' => 'required',
'datefilter' => 'required',
]);
$dep =\DB::table('workers')->select('workers.area_id')
->where(['workers.id' => $request->worker_id])
->get();
$vacation = new Vacation();
$vacation -> worker_id = $request['worker_id'];
$vacation -> type = $request['type'];
$vacation -> observations = $request['observations'];
$vacation -> area_id = $dep[0]->area_id;
//parse dates
$date = explode('-',$request['datefilter']);
$dateFrom = date("y-m-d",strtotime( $date[0]));
$dateTo = date("y-m-d",strtotime($date[1]));
$vacation -> date_from = date("y-m-d", strtotime($dateFrom));
$vacation -> date_to = date("y-m-d", strtotime( $dateTo ));
$guardadas_datefrom = \DB::table('vacations')
->where('date_from', $dateFrom)
->where('area_id', $dep[0]->area_id )
->get();
$guardadas_dateto = \DB::table('vacations')
->where('date_to', $dateTo)
->where('area_id', $dep[0]->area_id )
->get();
if(!$guardadas_datefrom && !$guardadas_dateto){
$vacation->save();
$data = request()->all();
return back()->with('success','Vacaciones Solicitadas correctamente');
}else{
return back()->with('error','Fechas no disponibles');
}
return back();
}
Thanks in advance. Greetings.