如何將像(res_id
,resname
等)參數傳遞給DataTable Service Class
(RestaurantDataTable
)用於自定義數據庫查詢(例如,只能獲得id = x
或resname = xxx
等的餐廳)?Laravel yajra Datatable:如何將參數發送到DataTable服務類以進行自定義查詢?
RestaurantController:
use App\DataTables\restaurantDataTable;
class restaurantController extends AppBaseController
{
public function index(restaurantDataTable $restaurantDataTable)
{
return $restaurantDataTable->render('restaurant.index');
}
}
restaurantDataTable:
class restaurantDataTable extends DataTable
{
/**
* @return \Illuminate\Http\JsonResponse
*/
public function ajax()
{
return $this->datatables
->eloquent($this->query())
->addColumn('action', 'restaurant.datatables_actions')
->make(true);
}
/**
* Get the query object to be processed by datatables.
*
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
*/
public function query()
{
$restaurants = restaurant::query();
return $this->applyScopes($restaurants);
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\Datatables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->addAction(['width' => '10%'])
->ajax('')
->parameters([
'dom' => 'Blfrtip',
'scrollX' => false,
'buttons' => [
'print',
'reset',
'reload',
[
'extend' => 'collection',
'text' => '<i class="fa fa-download"></i> Export',
'buttons' => [
'csv',
'excel',
'pdf',
],
],
'colvis'
]
]);
}
/**
* Get columns.
*
* @return array
*/
private function getColumns()
{
return [
'res_name' => ['name' => 'res_name', 'data' => 'res_name'],
'res_address' => ['name' => 'res_address', 'data' => 'res_address'],
'res_state' => ['name' => 'res_state', 'data' => 'res_state'],
'res_location' => ['name' => 'res_location', 'data' => 'res_location'],
'res_area' => ['name' => 'res_area', 'data' => 'res_area']
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'restaurant';
}
}
Table.blade.php:
@section('css')
@include('layouts.datatables_css')
@endsection
{!! $dataTable->table(['width' => '100%']) !!}
@section('scripts')
@include('layouts.datatables_js')
{!! $dataTable->scripts() !!}
@endsection
我需要自定義功能UsersDataTable
類像public function custom_query($param) {}
或public function custom_ajax($param) {}
?