Frank
Frank

Reputation: 99

Laravel: How to show a list of available time slots

Making a Laravel project where people can reserve some products for 1.5 hours.

In my controller, I am querying a variable of time slots that have already been reserved.

Next I want to withdraw those time slots out of the available time slots.

$filteredTimeSlots = DB::table('timeslots')
        ->join('reservations', 'timeslots.id', '=', 'reservations.timeslot_id')
        ->where('product_id', $product->id)
        ->get();

    $timeslots = Timeslot::all();

Right now $timeslots holds all the time slots. But in here are also those slots that have been specified in $filteredTimeSlots.

In total I have 6 products. Each can have reservations at the same time slots. So that's why I added the 'where' statement.

It's probably an easy fix but could not seem to find a clear solution on the Laravel documentation.

Thanks in advance.

Upvotes: 0

Views: 2061

Answers (1)

Matt Jameson
Matt Jameson

Reputation: 582

To find the remaining slots you want to make use of a left join where a slot hasnt been filled:

$timeslotsRemaining = DB::table('timeslots')
    ->leftJoin('reservations', 'timeslots.id', '=', 'reservations.timeslot_id')
    ->whereNull('reservations.timeslot_id')
    ->get();

Upvotes: 2

Related Questions