In this line you're using room_number' where in the form it's room_id`
$selectedRoom = Room::find($request->query('room_number'));
Change it to:
$selectedRoom = Room::findOrFail($request->query('room_id'));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
It's a classic error, but i just can't seem to find the bug.
I am trying to submit my form:
<form action="{{ route("doBooking") }}">
@csrf
<input type="hidden" name="user_id" id="user_id_value" value="{{$user->id}}">
<input type="hidden" name="room_id" id="room_id_value">
<input type="hidden" name="start_time" id="start_time_value">
<input type="hidden" name="end_time" id="end_time_value">
<button type="submit">
</button>
</form>
I am capturing all of the information just fine here. But i can't submit it. When i try, i'll get an "Attempt to read property "id" on null"-error, and it specifically marks this part of my code:
$bookings = Booking::where('room_id', $selectedRoom->id)->get();
This is what i have inside my user_booking function in my controller:
public function user_booking(Request $request, $id)
{
$rooms = Room::all();
$user = User::findOrFail($id);
$weekdays = WeekdayFactory::getWeekFromDate(new Carbon());
$selectedRoom = Room::find($request->query('room_number'));
$bookings = Booking::where('room_id', $selectedRoom->id)->get();
return view('booking.home')->with([
'weekdays' => $weekdays,
'user' => $user,
'rooms' => $rooms,
'roomSelector' => $selectedRoom,
'bookings' => $bookings
]);
}
I have tried to dd $selectedRoom and $bookings, and both contains information just fine.
This is what i have inside my user_booking function in my controller:
public function user_booking(Request $request, $id)
{
$rooms = Room::all();
$user = User::findOrFail($id);
$weekdays = WeekdayFactory::getWeekFromDate(new Carbon());
$selectedRoom = Room::find($request->query('room_number'));
$bookings = Booking::where('room_id', $selectedRoom->id)->get();
return view('booking.home')->with([
'weekdays' => $weekdays,
'user' => $user,
'rooms' => $rooms,
'roomSelector' => $selectedRoom,
'bookings' => $bookings
]);
}
Lastly, if it is any help, this is what i have inside my doBooking function:
public function doBooking(Request $request)
{
$validatedData = $request->validate([
'user_id' => 'required',
'room_id' => 'required',
'start_time' => 'required|before:end_time',
'end_time' => 'required|after:start_time',
]);
$bookings = Booking::all();
foreach ($bookings as $booking) {
if ($booking->room_id == $validatedData['room_id']) {
$validateBooking = Booking::whereBetween('start_time', [$validatedData['start_time'], $validatedData['end_time']])
->orWhereBetween('end_time', [$validatedData['start_time'], $validatedData['end_time']])
->exists();
if (!$validateBooking) {
Booking::create($validatedData);
} else {
return redirect()->back();
}
}
}
return view('/');
}
Oh yes, and i have controlled that my route exists, and it looks like this:
Route::post('/booking/create', [BookingsController::class, 'doBooking'])->name('doBooking');
In this line you're using room_number' where in the form it's room_id`
$selectedRoom = Room::find($request->query('room_number'));
Change it to:
$selectedRoom = Room::findOrFail($request->query('room_id'));
Please or to participate in this conversation.