One solution to this problem is to use a subquery to get all the taken bed IDs for a given room, and then use a whereNotIn clause to exclude those IDs from the list of available beds. Here's an example implementation:
// Controller
public function rooms(Bed $bed)
{
$rooms = Room::all();
$takenBeds = Bed::where('room_id', $bed->room_id)
->whereNotNull('participant_id')
->pluck('id');
$availableBeds = Bed::where('room_id', $bed->room_id)
->whereNotIn('id', $takenBeds)
->pluck('id');
return view('rooms', compact('availableBeds'));
}
// Blade
@foreach($availableBeds as $bedId)
{{ $bedId }}
@endforeach
In this implementation, we first get all the rooms and then use a subquery to get all the taken bed IDs for the given room. We then use a whereNotIn clause to exclude those IDs from the list of available beds, which we return to the view. In the view, we simply loop through the available bed IDs and display them.