在模型中为函数调用null上的成员函数get()

时间:2019-03-09 17:59:46

标签: laravel-5

我试图让一个雇员的按钮在变量中显示“已预订”文本 count($ bookingRequests)返回值,如果没有显示则显示“ Book Me”

我收到上面的错误。

我有以下模型及其关系:

User.php和Bookings.php

用户:

public function bookings() {

        return $this->hasMany('App\Models\Bookings');

}   


public function BookingRequest()

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


}

预订:

public function user(){

    return $this->belongsTo('App\Models\User');

}

上面提到的视图是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>

哪些数据是由控制器SearchController传递给它的:

通过以下方式:

       <?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具有以下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 个答案:

没有答案