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

swoop's avatar
Level 16

Check if relation exist and mark as Booked.

I'm new to Laravel, so bear with me if this is the easiest thing in the world. ;)

I've started on a booking system as my first test project learning Laravel.

I have Bookable items that have bookable_slots(time) for each day. And of course the slots can be booked by a user.

So without including every detail in this post.

The bookable_slots table:

id  | bookable_id | day | start_at
--- | ----------- | --- | --------
1   | 1           | 1   | 08:00:00
2   | 1           | 1   | 12:00:00
3   | 1           | 1   | 16:00:00

I know the 'day' and want to list all bookable slots for it. That part is easy and I've done it like this:

public static function getDaySlots($day)
    {
        $dayBookables = Bookable_slots::where('day', '=', $day->dayOfWeek)->get();

        return $dayBookables;
    }

! Notice that the 'start_at' field is a TIME column. So I guess I have to add the "chosen date" from the $day variable to the slots 'start_at' value before I can check.

Anyway.. I have a bookings table where the 'start_at' field is a DATETIME column:

id  | slot_id | start_at
--- | ------- | -------------------
1   | 1       | 2015-10-05 08:00:00
1   | 3       | 2015-10-05 16:00:00

The relations in the models:


// Bookable_slots

public function bookings()
    {
        return $this->hasMany('App\Booking', 'slot_id');
    }


// Booking

public function slot()
    {
        return $this->hasOne('App\BookableSlots');
    }


Now.. How can I check if there is a booking on a slot, on a specific date and mark it as booked in the getDaySlots return?

0 likes
0 replies

Please or to participate in this conversation.