feat: enhance product datatable with bulk delete functionality and refined category/price columns.
This commit is contained in:
@@ -127,23 +127,80 @@ class ProductController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function duplicate($id)
|
||||
{
|
||||
try {
|
||||
$product = $this->productRepository->find($id);
|
||||
|
||||
if (!$product) {
|
||||
return back()->withErrors("Can't find product.");
|
||||
}
|
||||
|
||||
$product->load(['pricings', 'osVersions', 'controlPanels']);
|
||||
|
||||
$newData = $product->only([
|
||||
'short_description', 'description', 'setup_fee', 'trial_days',
|
||||
'cpu', 'ram', 'gpu', 'disk', 'bandwidth', 'stock',
|
||||
'is_unlimited', 'is_active', 'sort_order', 'product_type',
|
||||
'product_categories_id', 'published_at',
|
||||
]);
|
||||
$newData['name'] = $product->name . ' (Copy)';
|
||||
|
||||
$newProduct = $this->productService->store($newData);
|
||||
|
||||
if ($product->osVersions->isNotEmpty()) {
|
||||
$newProduct->osVersions()->sync($product->osVersions->pluck('id'));
|
||||
}
|
||||
|
||||
if ($product->controlPanels->isNotEmpty()) {
|
||||
$syncData = [];
|
||||
foreach ($product->controlPanels as $panel) {
|
||||
$syncData[$panel->id] = [
|
||||
'price_modifier' => $panel->pivot->price_modifier,
|
||||
'is_default' => $panel->pivot->is_default,
|
||||
];
|
||||
}
|
||||
$newProduct->controlPanels()->sync($syncData);
|
||||
}
|
||||
|
||||
if ($product->pricings->isNotEmpty()) {
|
||||
foreach ($product->pricings as $pricing) {
|
||||
$newProduct->pricings()->create([
|
||||
'billing_cycle' => $pricing->billing_cycle,
|
||||
'price' => $pricing->price,
|
||||
'price_sale' => $pricing->price_sale,
|
||||
'currency' => $pricing->currency,
|
||||
'is_active' => $pricing->is_active,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('products.edit', ['id' => $newProduct->id])
|
||||
->with('success', 'Product duplicated successfully');
|
||||
} catch (Exception $e) {
|
||||
return back()->withErrors($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function datatable(Request $request)
|
||||
{
|
||||
$query = $this->productRepository->filter(
|
||||
$request->all(),
|
||||
['category'],
|
||||
['category', 'pricings'],
|
||||
[],
|
||||
['*'],
|
||||
true
|
||||
);
|
||||
|
||||
return DataTables::eloquent($query)
|
||||
->addColumn('category_name', function ($d) {
|
||||
return $d->category?->name ?? '-';
|
||||
->addColumn('checkbox', function ($d) {
|
||||
return '<input type="checkbox" class="form-check-input dt-checkbox" value="' . $d->id . '">';
|
||||
})
|
||||
->addColumn('product_type_label', function ($d) {
|
||||
$type = ProductTypeEnums::tryFrom($d->product_type);
|
||||
return $type ? $type->label() : '-';
|
||||
->addColumn('category', function ($d) {
|
||||
return view('administrator.pages.product.datatable.category', ['row' => $d])->render();
|
||||
})
|
||||
->addColumn('price', function ($d) {
|
||||
return view('administrator.pages.product.datatable.price', ['row' => $d])->render();
|
||||
})
|
||||
->addColumn('specs', function ($d) {
|
||||
return view('administrator.pages.product.datatable.specs', ['row' => $d])->render();
|
||||
@@ -157,7 +214,7 @@ class ProductController extends Controller
|
||||
return view('administrator.pages.product.datatable.action', ['row' => $d])->render();
|
||||
}
|
||||
)
|
||||
->rawColumns(['specs', 'status', 'action'])
|
||||
->rawColumns(['checkbox', 'category', 'price', 'specs', 'status', 'action'])
|
||||
->make(true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user