samedi 9 mars 2019

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

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('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>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire