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

screwtape_mk's avatar

Call to a member function get() on null

I am trying to have a button of an employee show the text “Already booked” if $bookingRequests has count not null OR ("Book me") if $bookingRequests has count=0 or null if the following variable is

count($bookingRequests)

I have the following models and their relationships:

User.php and Bookings.php

User:

public function bookings() {
        
            return $this->hasMany('App\Models\Bookings');
    
    }   


    public function BookingRequest()
    
    {
        $this->bookings()->where('booking_status','=','confirmed')->get();  
    
    
    }


Bookings:

public function user(){
    
        return $this->belongsTo('App\Models\User');
    
    }

The view mentioned above is Employeeblock.blade.php:

    <div class="row no-gutters mb-5 mb-lg-0" style="padding:10px">
                
                    @if(count($bookingRequests))
                        <a href="#" class="btn btn-primary" data-toggle="modal" data-    
                                        target=“#bookingModal">Already Booked</a>
                    @else
                
                        <a href="#" class="btn btn-primary" data-toggle="modal" data- 
                                        target="#bookingModal">Book Me</a>
                        
                    @endif
                
         </div>

Which has data passed onto it by a controller SearchController:

In the following way:

<?php 
namespace App\Http\Controllers;

use DB;
use Auth;
use App\Models\Employee;
use App\Models\User;
use App\Models\Bookings;

use Illuminate\Http\Request;

use GuzzleHttp\Client;
use Session;

.
.
.
$bookingRequests=Auth::user()->BookingRequest()->get();

    return view(‘search.results’)->with('bookingRequests',$bookingRequests);

results.blade.php above has the following html:

<div class="panel panel-default">
            <div class="panel-heading"><h3></h3></div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-12">
    
    
                                @foreach($Employees as $Employee)
                                        @include('user/partials/employeeblock')
                                @endforeach
    
    
        
                        </div>
                    </div>
                </div>
            </div>
</div>

0 likes
3 replies
Nee's avatar

Could you check if you have a booking record with booking_status being 'confirmed'?

nodenacci's avatar
Level 3

Hi there, your problem is in BookingRequest() method.

First, the method doesnt have a return statement. So nothing gets returned. Second, it has get() call which you call again on the controller.

To fix your issue, replace your method with the following:

public function BookingRequest()   
    {
        return $this->bookings()->where('booking_status','=','confirmed');
    
    
    }

Now your controller code with work perfectly returning the collection $bookingRequests which you pass to your view.

A bonus point, since $bookingRequests is a collection, replace @if(count($bookingRequests)) with @if($bookingRequests->count()) on your view.

2 likes
screwtape_mk's avatar

thanks so much super helpful!...cant believe i left out 'return'

Please or to participate in this conversation.