方法 Yajra\DataTables\CollectionDataTable::where 不存在

Method Yajra\DataTables\CollectionDataTable::where does not exist

我在 Laravel 项目中有一个数据表。我创建了一个下拉过滤器来过滤 KPI 列。 这是我的查看代码

$(function() {

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var table = $('.data-table').DataTable({
                paging: true,
                info: true,
                autoWidth: false,
                responsive: true,
                processing: true,
                serverSide: true,
                ajax: {
                    url: "{{ route('my.task') }}",
                    data: function(d) {
                        d.kpi = $('#filter-kpi').val(),
                            d.search = $('input[type="search"]').val()
                    }
                },

                columns: [{
                        data: 'DT_RowIndex',
                        name: 'DT_RowIndex',
                        orderable: false,
                        searchable: false,
                    },
                    {
                        data: 'title',
                        name: 'title',
                        orderable: false,
                    },
                    {
                        data: 'content',
                        name: 'content',
                        orderable: false,
                        visible: false,
                    },
                    {
                        data: 'progress',
                        name: 'progress'
                    },
                    {
                        data: 'status',
                        name: 'status'
                    },
                    {
                        data: 'kpi',
                        name: 'kpi'
                    },
                    {
                        data: 'target_selesai',
                        name: 'target_selesai'
                    },
                    {
                        data: 'action',
                        name: 'action',
                        orderable: false,
                        searchable: false
                    },
                ]
            });

            $('#createNewProduct').click(function() {
                ...
            });

            $('body').on('click', '.editProduct', function() {
               ...
            });

            $('body').on('click', '.detailProduct', function() {
               ...
            });

            $('#saveBtn').click(function(e) {
               ...
            });

            $('body').on('click', '.deleteProduct', function() {
               ...
                    });
                } else {
                    return false;
                }
            });


            $('#filter-kpi').change(function() {
                table.draw();
            });
        });

这是我的控制器

 public function MyTask(Request $request)
    {
        // if (Auth::user()->jabatan != "Staff") {
        //     abort(403);
        // }

        if ($request->ajax()) {
            //$data = Post::where('user_id', Auth::user()->uid)->latest()->get();
            $data = Post::select('*');
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function ($row) {
                   ... 
                })->addColumn('target_selesai', function ($row) {
                    
                })->addColumn('kpi', function ($row) {
                   ...                    
                })
                ->addColumn('progress', function ($row) {
                  ...
                    }
                })->addColumn('status', function ($row) {
                    ... 
                })->filter(function ($instance) use ($request) {
                    if ($request->get('kpi') == '0' || $request->get('kpi') == '1') {
                        $instance->where('kpi', $request->get('kpi'));
                    }
                    if (!empty($request->get('search'))) {
                        $instance->where(function ($w) use ($request) {
                            $search = $request->get('search');
                            $w->orWhere('title', 'LIKE', "%$search%")
                                ->orWhere('content', 'LIKE', "%$search%");
                        });
                    }
                })

                ->rawColumns(['action', 'progress', 'status'])
                ->make(true);
        }

        return view('task.mytask');
    }

我的下拉过滤器

<div class="form-group">
        <label><strong>KPI :</strong></label>
        <select id='filter-kpi' class="form-control" style="width: 200px" data-column="5">
            <option value="">Semua</option>
            <option value="1">KPI</option>
            <option value="0">Non-KPI</option>
        </select>
    </div>

当我使用 $data = Post::select('*'); 时,该程序按预期运行良好。但是,我想将其更改为 $data = Post::where('user_id', Auth::user()->uid)->latest()->get(); 但如果我使用下拉过滤器,它会弹出警告说“方法 Yajra\DataTables\CollectionDataTable:: 不存在的地方”

我的代码有问题吗?提前致谢

删除对 get() 的调用,这样您就可以:

$data = Post::where('user_id', Auth::user()->uid)->latest();

这将 return Eloquent 查询 Builder instance as opposed to a Collection 实例。

Datatables需要在后续的方法链中使用builder实例