Laravel Datatables, return only specific rows
I am using Yajra\Datatables\Services\DataTable
I have a controller with:
use App\DataTables\restaurantsDataTable;
class restaurantsController extends AppBaseController {
public function index(restaurantsDataTable $restaurantsDataTable)
{
return $restaurantsDataTable->render('restaurants.index');
}
....
}
and the App\DataTables\restaurantsDataTable with:
use App\Models\restaurants; use Form; use Yajra\Datatables\Services\DataTable;
class restaurantsDataTable extends DataTable {
/**
* @return \Illuminate\Http\JsonResponse
*/
public function ajax()
{
return $this->datatables
->eloquent($this->query())
->addColumn('action', 'restaurants.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 = restaurants::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' => 'Bfrtip',
'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_image' => ['name' => 'res_image', 'data' => 'res_image', 'visible' => false],
'res_image_date' => ['name' => 'res_image_date', 'data' => 'res_image_date', 'visible' => false],
'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'],
'res_telephone' => ['name' => 'res_telephone', 'data' => 'res_telephone', 'visible' => false],
'res_min_price' => ['name' => 'res_min_price', 'data' => 'res_min_price', 'visible' => false],
'res_max_price' => ['name' => 'res_max_price', 'data' => 'res_max_price', 'visible' => false],
'res_latitude' => ['name' => 'res_latitude', 'data' => 'res_latitude', 'visible' => false],
'res_longitude' => ['name' => 'res_longitude', 'data' => 'res_longitude', 'visible' => false],
'res_chain' => ['name' => 'res_chain', 'data' => 'res_chain', 'visible' => false],
'res_new' => ['name' => 'res_new', 'data' => 'res_new', 'visible' => false],
'res_offer' => ['name' => 'res_offer', 'data' => 'res_offer', 'visible' => false],
'res_type_state' => ['name' => 'res_type_state', 'data' => 'res_type_state', 'visible' => false]
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'restaurants';
}
}
How do I customize the:
public function index(restaurantsDataTable $restaurantsDataTable) { return $restaurantsDataTable->render('restaurants.index'); }
to return some table rows that matched some ids? like:
App\Models\restaurants::where('res_id', '=', 1)->get();
Please or to participate in this conversation.