I could not find an answer on my question so I hope someone can help me
I want to validate if I add a new appointment that the chosen employee has not been chosen on the day of the appointment. So I can't double-book someone on a day. I'm using laravel 5.6 and MySQL with table appointments using following rows: id, day, employee_id and resource_id
My controller is a resource controller (with the index,create,store,... functions).
So if $appointmentExists is 1 I need to throw an error and stay on the same page of the create form.
public function store(Request $request)
{
$appointmentExist = \DB::table('appointments')
->where([
['day','=',$request->day],
['employee_id','=',$request->employee_id],
])
->exists();
$request->validate([
'day' => 'required|min:1|max:10',
'employee_id' => 'required',
'resource_id' => 'required',
$appointmentExist => 'in:0',
]);
$appointment = Appointment::create(['day' => $request->day, 'employee_id' => $request->employee_id, 'resource_id' => $request->resource_id]);
return redirect('/appointments/' . $appointment->id);
}
I hope someone can help
[day, employee_id], then database definitely won't allow more than 1 record. Your next step is to insert the data. If record exists, Laravel will throw an exception. Code23000meansduplicate record. You use that to tell your user that an appointment has been booked.