Try to search your question here, if you can't find : Ask Any Question Now ?

Call to a member function get() on null for a function in a model

HomeCategory: stackoverflowCall to a member function get() on null for a function in a model
Avatargaurav asked 4 months ago

I am trying to have a button of an employee show the text “Already booked” if the variable
count($bookingRequests) returns something and if nothing then show “Book Me’

I get the error above.

I have the following models and their relationships:

User.php and Bookings.php

User:

public function bookings() {

        return $this->hasMany('AppModelsBookings');

}   


public function BookingRequest()

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


}

Bookings:

public function user(){

    return $this->belongsTo('AppModelsUser');

}

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 AppHttpControllers;

      use DB;
      use Auth;
      use AppModelsEmployee;
      use AppModelsUser;
      use AppModelsBookings;
      use IlluminateHttpRequest;

      use GuzzleHttpClient;
      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>
1 Answers
Best Answer
AvatarMikhail answered 4 months ago
Your Answer

20 + 3 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com