162 lines
5.4 KiB
PHP
162 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Brands\StoreBrandRequest;
|
|
use App\Http\Requests\Bo\Brands\UpdateBrandRequest;
|
|
use App\Models\Brand;
|
|
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 BrandController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Brand::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 = Brand::query()->withCount([
|
|
'ranges', 'products',
|
|
'ranges as published_ranges' => fn (Builder $q) => $q->where('ranges.status', true),
|
|
'products as published_products' => fn (Builder $q) => $q->where('products.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);
|
|
|
|
/** Custom pagination */
|
|
$brandModelList = $this->paginate($query);
|
|
|
|
return view('bo.pages.brands.index', compact('brandModelList', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.brands.create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\StoreBrandRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreBrandRequest $request): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$brand = new Brand();
|
|
$brand->fill($request->validated());
|
|
|
|
if ($brand->saveOrFail()) {
|
|
return redirect()->route('bo.brands.edit', $brand)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Brand $brand): View
|
|
{
|
|
$brand->loadCount([
|
|
'ranges', 'products',
|
|
'ranges as published_ranges' => fn (Builder $q) => $q->where('ranges.status', true),
|
|
'products as published_products' => fn (Builder $q) => $q->where('products.status', true),
|
|
]);
|
|
return view('bo.pages.brands.edit', ['brandModel' => $brand]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\UpdateBrandRequest $request
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateBrandRequest $request, Brand $brand): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $brand) {
|
|
$brand->fill($request->validated());
|
|
|
|
if ($brand->saveOrFail()) {
|
|
return redirect()->route('bo.brands.edit', $brand)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.brands.edit', $brand)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Brand $brand): RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($brand->deleteOrFail()) {
|
|
return redirect()->route('bo.brands.index', $previousQueries)
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1))
|
|
]));
|
|
}
|
|
}
|