180 lines
6.1 KiB
PHP
180 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Products\StoreProductRequest;
|
|
use App\Http\Requests\Bo\Products\UpdateProductRequest;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Product::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $query */
|
|
$query = Product::query()->with(['range', 'brand'])->withCount([
|
|
'range as parent_published' => fn (Builder $q) => $q->where('ranges.status', true),
|
|
'range as parent_shelf' => fn (Builder $q) => $q->where('ranges.shelf', true),
|
|
'brand as parent_parent_published' => fn (Builder $q) => $q->where('brands.status', true),
|
|
]);
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->input('search');
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.name'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
if ($request->input('range_id')) {
|
|
$query->whereHas('range', fn (Builder $q) => $q->where('id', $request->input('range_id')));
|
|
}
|
|
|
|
/** Custom pagination */
|
|
$productModelList = $this->paginate($query);
|
|
$rangeList = Range::query()->pluck('name', 'id');
|
|
|
|
return view('bo.pages.products.index', compact(
|
|
'productModelList',
|
|
'rangeList',
|
|
'search',
|
|
'searchFields'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(): \Illuminate\Contracts\View\View
|
|
{
|
|
$rangeModelList = Range::all();
|
|
return view('bo.pages.products.create', ['rangeModelList' => $rangeModelList]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Products\StoreProductRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreProductRequest $request): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$product = new Product();
|
|
$product->fill($request->validated());
|
|
|
|
if ($product->saveOrFail()) {
|
|
return redirect()->route('bo.products.edit', $product)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.product', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.product', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Product $product): View
|
|
{
|
|
$product->load([
|
|
'range', 'brand'
|
|
]);
|
|
$product->loadCount([
|
|
'range as parent_published' => fn (Builder $q) => $q->where('ranges.status', true),
|
|
'brand as parent_parent_published' => fn (Builder $q) => $q->where('brands.status', true),
|
|
]);
|
|
$rangeModelList = Range::all();
|
|
return view('bo.pages.products.edit', [
|
|
'rangeModelList' => $rangeModelList,
|
|
'productModel' => $product
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Products\UpdateProductRequest $request
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateProductRequest $request, Product $product): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $product) {
|
|
$product->fill($request->validated());
|
|
|
|
if ($product->saveOrFail()) {
|
|
return redirect()->route('bo.products.edit', $product)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.product', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.products.edit', $product)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.product', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Product $product): RedirectResponse
|
|
{
|
|
if ($product->deleteOrFail()) {
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
return redirect()->route('bo.products.index', $previousQueries)
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.product', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.product', 1))
|
|
]));
|
|
}
|
|
}
|