I'm struggling to define a couple of nested if statements for a project I'm working on; a way for employees to book desks in different rooms of an office.
Model relationships:
- Room hasMany Desks
- Desk belongsTo Room
- Booking hasOne Desk (vice-versa)
- Booking hasOne User (vice-versa)
I want to provide an index of each of the room cards, each with respective clickable desks indexed in a grid within the card. The auth user can click on a desk and confirm thier booking.
There are 4 states that the rendered desk card can have; available to book, disabled due to booking already made by auth user, unavailable having been booked by another user, and booked by auth user.
The bookings table migration looks like this:
Schema::create('bookings', function (Blueprint $table) {
$table->id();
$table->foreignId('desk_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->foreignId('room_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
RoomController
public function index()
{
return view('index', [
'rooms' => Room::with("desks")->get(),
'desks' => Desk::all(),
'user' => Auth::user(),
'bookings' => Booking::all()
]);
}
The index view with the if statements in question is below:
@if ($rooms->count())
<div class="grid grid-cols-1 gap-6 mt-5 pb-20">
@foreach ($rooms as $room)
<div class="bg-white h-auto rounded-xl shadow-inner">
<div class="mx-3 my-4">
<div class="mt-2 text-2xl font-semibold text-gray-800 text-left pl-5">
</div>
<div class="lg:grid lg:grid-cols-5 gap-5 mt-4">
@foreach ($desks->where('room_id', $room->id) as $desk)
// @if(desk_id exists in the bookings table)
// @if(user_id of said desk belongs to auth user)
<x-grid.desks.my-booking :desk='$desk' :room="$room" :user="$user" />
@else
<x-grid.desks.booked :desk='$desk' :room="$room" :user="$user" />
@endif
@endif
// @if(desk_id exists in desks table and doesn't exist in Bookings table)
// @if(auth user id exists in bookings)
<x-grid.desks.unavailable :desk='$desk' :room="$room" :user="$user" />
@else
<div x-data="{show: false}">
<x-grid.desks.available :desk='$desk' :room="$room" :user="$user" />
</div>
@endif
@endif
@endforeach
</div>
</div>
</div>
@endforeach
</div>
@else
<p class="text-center text-xl mt-10">No bookings available yet. Please check back later!</p>
@endif
I've previously attempted to write the if statements without any success, so they're written in plain english for now. Much appreciated if someone could lend a hand, thanks.
Aucun commentaire:
Enregistrer un commentaire