171 lines
5.7 KiB
PHP
171 lines
5.7 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\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $productModels */
|
|
$productModels = Product::query()->with('range');
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$productModels,
|
|
$search,
|
|
null,
|
|
'name',
|
|
'code_ean',
|
|
);
|
|
}
|
|
$searchFields = \sprintf(
|
|
'%s, %s ...',
|
|
Str::ucfirst(__('validation.attributes.name')),
|
|
__('validation.custom.ean_code'),
|
|
);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($productModels);
|
|
|
|
/** Custom pagination */
|
|
$productModels = $this->paginate($productModels);
|
|
|
|
return view('back::pages.products.index', compact('productModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function create(Product $product): \Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
{
|
|
$rangeModels = Range::all();
|
|
if (!count($rangeModels)) {
|
|
return \redirect()->route('bo.ranges.index')
|
|
->with(
|
|
'error',
|
|
'Vous devez ajouter une gamme avant de pouvoir ajouter un produit'
|
|
);
|
|
}
|
|
return view('back::pages.products.create', [
|
|
'productModel' => $product,
|
|
'rangeModels' => $rangeModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Products\StoreProductRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreProductRequest $request): \Illuminate\Http\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', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Product $product): \Illuminate\Contracts\View\View
|
|
{
|
|
$rangeModels = Range::all();
|
|
return view('back::pages.products.edit', [
|
|
'productModel' => $product,
|
|
'rangeModels' => $rangeModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $product) {
|
|
$product->fill($request->validated());
|
|
if ($product->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.products.edit', $product)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Product $product): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($product) {
|
|
if ($product->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.products.index')
|
|
->with('success', __(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', __(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
});
|
|
}
|
|
}
|