eloquent($query)
->addColumn('action', function ($data) {
return view('product::products.partials.actions', compact('data'));
})
->addColumn('product_image', function ($data) {
$url = $data->getFirstMediaUrl('images');
return '
';
})
->addColumn('product_name', function ($data) {
return ''.$data->product_name.'';
})
->addColumn('total_quantity', function ($data) {
return $data->product_quantity;
})
->rawColumns(['product_image', 'product_name', 'action']);
}
public function query(Product $model)
{
return $model->newQuery()
->with('category')
->leftJoin(DB::raw('(SELECT product_id, SUM(quantity) as total_quantity FROM product_batches GROUP BY product_id) as pb'), 'products.id', '=', 'pb.product_id')
->select('products.*', DB::raw('COALESCE(pb.total_quantity, 0) as product_quantity'));
}
public function html()
{
return $this->builder()
->setTableId('product-table')
->columns($this->getColumns())
->minifiedAjax()
->dom("<'row'<'col-md-3'l><'col-md-5 mb-2'B><'col-md-4'f>> .
'tr' .
<'row'<'col-md-5'i><'col-md-7 mt-2'p>>")
->orderBy(4)
->buttons(
Button::make('excel')
->text(' Excel'),
Button::make('print')
->text(' Print'),
Button::make('reset')
->text(' Reset'),
Button::make('reload')
->text(' Reload')
);
}
protected function getColumns()
{
return [
Column::make('product_image')
->title('Image')
->className('text-center align-middle'),
Column::make('product_name')
->title('Name')
->className('text-center align-middle'),
Column::make('product_code')
->title('Code')
->className('text-center align-middle'),
Column::make('category.category_name')
->title('Category')
->className('text-center align-middle'),
Column::make('product_unit')
->title('Unit')
->className('text-center align-middle'),
Column::make('total_quantity')
->title('Quantity')
->className('text-center align-middle'),
Column::computed('action')
->exportable(false)
->printable(false)
->className('text-center align-middle')
->addClass('text-center'),
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename(): string
{
return 'Product_' . date('YmdHis');
}
}