Laravel BadMethodCallException Macroable.php中不存在已确认的方法(查询范围)(第74行)

时间:2017-07-29 15:05:01

标签: php laravel datatables eloquent

这是我的模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;


class Jadwal extends Model
{

    protected $fillable = ['ruang_id', 'tanggal', 'sesi_id', 'acara', 'user_id', 'pj_nama', 'pj_id', 'keterangan', 'alat', 'status'];


    public function ruang()
    {
        return $this->belongsTo('App\Ruang');
    }

    public function sesi()
    {
        return $this->belongsTo('App\Sesi');
    }

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

    public function scopeBook($query)
    {
        return $query->where('status', 0);
    }

    public function scopeConfirmed($query)
    {
       return $query->where('status', 1);
    }
}

这是我对脚本的看法:

@extends('layouts.app')

@section('content')
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h2 class="panel-title">Daftar Jadwal Ruang</h2>
          </div>

          <div class="panel-body">
            {!! $html->table(['class'=>'table-striped']) !!}
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

@section('scripts')
  {!! $html->scripts() !!}
  <script>
    $(function() {
      $('\
        <div id="filter_stat" class="dataTables_length" style="display: inline-block; margin-left:10px;">\
          <label>Status \
          <select size="1" name="filter_stat" aria-controls="filter_stat" \
            class="form-control input-sm" style="width: 140px;">\
              <option value="all" selected="selected">Semua</option>\
              <option value="booking">Booking</option>\
              <option value="confirmed">Confirmed</option>\
            </select>\
          </label>\
        </div>\
      ').insertAfter('.dataTables_length');

      $("#dataTableBuilder").on('preXhr.dt', function(e, settings, data) {
        data.stat = $('select[name="filter_stat"]').val();
      });

      $('select[name="filter_stat"]').change(function() {
        window.LaravelDataTables["dataTableBuilder"].ajax.reload();
      });
    });
  </script>
@endsection

这是我的控制器:

public function index(Request $request, Builder $htmlBuilder)
    {
        if ($request->ajax()) {
            $jadwals = Jadwal::with('ruang', 'sesi', 'user')->get();
            if ($request->get('stat') == 'booking') $jadwals->book();
            if ($request->get('stat') == 'confirmed') $jadwals->confirmed();
            return Datatables::of($jadwals)->make(true);
        }
        $html = $htmlBuilder
        ->addColumn(['data' => 'ruang.nama', 'nama'=>'ruang.nama', 'title'=>'Ruang'])
        ->addColumn(['data' => 'tanggal', 'nama'=>'tanggal', 'title'=>'Tanggal'])
        ->addColumn(['data' => 'sesi.nama', 'nama'=>'sesi.nama', 'title'=>'Sesi'])
        ->addColumn(['data' => 'acara', 'nama'=>'acara', 'title'=>'Acara'])
        ->addColumn(['data' => 'user.name', 'nama'=>'user.name', 'title'=>'Pengguna'])
        ->addColumn(['data' => 'status', 'nama'=>'status', 'title'=>'Status']);
        return view('guests.index')->with(compact('html'));
    }
  

当我在修补器上测试方法时。那是工作!但是当我执行它时   在浏览器上,方法&#39;确认&#39;不存在。

PS C:\xampp\htdocs\RoomScheduling> php artisan tinker
Psy Shell v0.8.10 (PHP 7.0.5 ÔÇö cli) by Justin Hileman
>>> App\Jadwal::confirmed()->get();
=> Illuminate\Database\Eloquent\Collection {#865
     all: [
       App\Jadwal {#867
         id: 2,
         ruang_id: 2,
         tanggal: "2017-07-31",
         sesi_id: 1,
         acara: "Seminar",
         user_id: 10,
         pj_nama: "Hanafi",
         pj_id: "2016",
         foto_identitas: "cf7d249c202edd43b5b0424722f847b4.png",
         keterangan: "Mohon ijinnya.",
         alat: "Mic",
         status: 1,
         created_at: "2017-07-28 17:28:13",
         updated_at: "2017-07-29 09:13:58",
       },
     ],
   }

Error BadMethodCallException

  

请帮帮我。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您在变量confirmed()中的对象上调用$jadwals,但在此变量中您有Illuminate\Database\Eloquent\Collection,因为您在此行中调用了get()

$jadwals = Jadwal::with('ruang', 'sesi', 'user')->get();

您必须在查询上执行范围,而不是在集合上执行。只需删除get()并修复下一行即可考虑此更改。