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

Gamborgc's avatar

Can't find error in Attempt to read property "ID" on null

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');
0 likes
4 replies
MohamedTammam's avatar
Level 51

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')); 
Gamborgc's avatar

It has to be room_number since my query is setup like that. I did however change it to findOrFail, and got a 404 returned. Edit: Does it change anything? I thought that query('room_number') was looking for the key in the url where $selectedRoom will get its value from?

Snapey's avatar

You miss the point. Your form field is room_id. Your controller is not going to find any record using $request->query('room_number') because no such field is in the form.

Gamborgc's avatar

So i figured out the problem, and i almost forgot to respond! :-) For people how might have the same problem as me, MohamedTammam and Snapey were completly right. I had to change this: $selectedRoom = Room::find($request->query('room_number'));

To this: $selectedRoom = Room::findOrFail($request->query('room_id'));

At first, it did not make it better, but that was because i needed to correct the url that i was constructing with my javascript. My url looked like this: http://127.0.0.1:8000/booking/1?room_number=114 but i had to make it into this instead: http://127.0.0.1:8000/booking/1?room_id=114.

The main takeaway for me was to look inside my Room model, and see the actual name, which looks like this:

protected $fillable = [
    'room_id'
];

Thank you for your help! :-)

Please or to participate in this conversation.