This is the most important part of this boilerplate. We can generate datatable with search, filter, with server side pagination very easily with Laravel premio. Just follow the below steps:
Assume we are going to fecth Page
model data.
public function index(Request $request)
{
if ($request->ajax()) {
return $this->table(Page::query())
->addColumn('action', function ($row) {
return action_button([
'first_link' => [
'route' => route('page.edit',$row->slug),
'is_modal' => false, //page open without modal
'button_text' => 'Edit'
],
'second_link' => [
'route' => route('page.delete',$row->slug),
'true' => false, //page open with modal
'button_text' => 'Trash'
],
]);
})
->editColumn('name', function($row){
if($row->status == true){
return $row->name . " <span class='text-primary'>(Published)</span>";
}else{
return $row->name ." <span class='text-info'>(Draft)</span>";
}
})
->escapeColumns('name') //if you use `HTML` like `name` column, you need to scape using `escapeColumns`
->rawColumns(['action'])
->make(true);
}
return view('admin.page.index');
}
Now generate datatable in your page index page like this
<x-app-component>
<x-page.page-title data="Pages" />
<x-slot name="style">
<link rel="stylesheet" href="{{ mix('css/datatable.css') }}" />
</x-slot>
<x-slot name="content">
<div class="card">
<div class="card-body">
//the column you want to show
{{ generate_table_columns(['name', 'created', 'last modified', 'action']) }}
</div>
</div>
@include('admin.modal._export', ['db_table' => 'pages']) //pass table name to export it
</x-slot>
<x-slot name="script">
<script src="{{ mix('js/datatable.js') }}"></script>
<script>
const columns = ['id', 'name', 'created_at', 'updated_at']; //the exact column name you want to display
const order = "desc"; //you can pass order by 'desc' and 'asc'
const route = "{{ route('page.index') }}"; //route
const button = true; //if you pass false then action button does not show.
const table = 'Page'; //Model name
generate_datatable(route, columns, order, button, table);
</script>
</x-slot>
</x-app-component>