Hello, I want to create a booking system where user chooses Date and one of the timeslots given. My idea right now is to store in database table, the booking_date in one field and the booking_timeslot in another. The problem with this design is that I do not know how to prevent another user from choosing the same date and timeslot, or how to render the timeslot chosen on a particular day non-usable again.
Please I would really appreciate any advice. Thank you in advance.
I have worked on a similar project in the past, I booking slots like you (i was working with hour time slots). I ran used a table for slots and a table/model for slot booking.
The slots had a one to many relationship to the slot booking
when loading the view I had a while loop for every slot and then checked if they have any slot bookings (for the specific date), I then created a new collection SlotAvailability with the slot type and other details about the slot.
in the view, I looped through the SlotAvailability and had an if statement to show dependant on the availability of the slot
@if($availability->status == 1)
Available
@elseif($availability->status == 2)
booked
@endif
@morganc3 thank you for your response, how do we make sure that we are checking the availability of slots for a specific date? And how did the SlotAvailability work? Sorry if this is a stupid question, and I really appreciate your help!
my slots table had one set of slots (slots were the same for each day) (every hour from 8 am till 11 pm) then when a user booked a slot I created a new slot record with the id of the slot (so I could have all the slot details), and the specific date of the booking so then I could pull all the booked slots for a specific date.
then when your checking the availability you can just pull all the slots then pull all the slot bookings for the specific date.
use Carbon if you're not already it's excellent for date and time
so in the slot controller (where i load the data and view)
i pull all the data
$slots = Slot::all();
$slotAvailability = collect(new SlotAvailability());
foreach($slots as $slot){
//Create a new model (Slot Availability)
$availability = new SlotAvailability();
$availability->slotID = $slot->id;
// I had price as it was a delivery app and diffrent slots had differnt prices
$availability->price = 5;
$availability->status = 1;
$availability->date = $date;
// added it to the collectio
$slotAvailability->add($availability);
}
// Pass the data to the view
return view('shop.checkout.slot')->with([
'slots' => $slots,
'slotAvailability' => $slotAvailability,
]);
in the view
@foreach($slots as $slot)
@foreach ($slotAvailability->where('slotID', $slot->id) as $availability)
//add the if statement from above
@endforeach
@endforeach