Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

karresachi's avatar

Bookings create, is this solution good?

Hello, I am creating a booking application for businesses and now I am creating the booking time slots.

Example. A business (user) logs in to the app and creates available times for a specific service. The form looks like this and lets say the user types in the following inputs:

1. Choose Service: Bowling  
2. Choose time: 10:00 and 12:00  
3. Booking avaiable online: Yes // or No

Note: Bowling has a duration of 60 minutes (this is added before)

Now, when the user clicks on save this function is triggered (see below)

public function createBookings(Request $request)
    {

        $start_time = now()->addHours(rand(1,100));
        $six_digit_random_number = mt_rand(100000, 999999);

        $booking = new Booking();
        $booking->business_id = 1;
        $booking->service_id = $request->service_id;
        $booking->employee_id = 1;
        $booking->customer_id = null;
        $booking->start_time = $start_time->format('Y-m-d H') . ':00';
        $booking->end_time = $start_time->addMinutes(rand(45, 46))->format('Y-m-d H') . ':00';
        $booking->booking_number = $six_digit_random_number;
        $booking->comment = null;
        $booking->status = 0;
        $booking->save();

        return response()->json();
    }

Just testing this out so some values are not dynamic.

My solution now is that if the user chooses the time 10:00 and 12:00 (as the example above) I am going to create in the table bookings 2 months of available bookings for each day and for the time. So after the function is called it will generate 60 different days for each booking time 10:00 and 12:00

For example in the bookings table:

id | "foreign keys" | start_time | end_time | booking_number | comment | status  
1  | "foreign_keys" | 2021-03-31 10:00 | 2021-03-31 11:00 | 100000 | null | 0  
2  | "foreign_keys" | 2021-04-01 10:00 | 2021-04-01 11:00 | 100001 | null | 0  
...  
60  | "foreign_keys" | 2021-05-30 10:00 | 2021-05-30 11:00 | 100060 | null | 0  <--- now created 60 days for time *10:00* with a duration of 60 minutes  
61  | "foreign_keys" | 2021-03-31 12:00 | 2021-03-31 13:00 | 100061| null | 0  
62  | "foreign_keys" | 2021-04-01 12:00 | 2021-04-01 13:00 | 100062 | null | 0  
...  
120  | "foreign_keys" | 2021-05-30 12:00 | 2021-05-30 13:00 | 100120 | null | 0  <--- now created 60 days for time *12:00* with duration of 60 minutes
 

* "foreign_keys" are business_id service_id employee_id customer_id, to lazy to write all of them

So for the question, is this solution good? Or is there anything else I can do? If anyone could give me some input that would be great.

0 likes
1 reply
jlrdw's avatar

There are also packages on Github for "booking", you could also browse over some of the code and gather more ideas. Also there is laravel calendar that can help with this.

Last one I did, I had set time slots per business day, and just filled the slot. Was searchable via id or last name.

But looks like you are on the right track, just remember the keep it simple thing. The less relations you implement the easier a query will be.

Please or to participate in this conversation.