Reputation: 81
I have a datatable in Laravel Project. I've create a dropdown filter to filter KPI column. Here is my view code
$(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();
});
});
And here is my controller
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');
}
My dropdown filter
<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>
When I use $data = Post::select('*');
the program works well as expected. But, I want to change it into $data = Post::where('user_id', Auth::user()->uid)->latest()->get();
but if I use the dropdown filter it will pop up alert said "Method Yajra\DataTables\CollectionDataTable::where does not exist"
Is there something wrong in my code? thanks in advance
Upvotes: 4
Views: 10328
Reputation: 18197
Remove the call to get()
so that you have:
$data = Post::where('user_id', Auth::user()->uid)->latest();
This will return the Eloquent query Builder instance as opposed to a Collection instance.
Datatables needs to use the builder instance in the subsequent method chain.
Upvotes: 9