176 lines
5.5 KiB
PHP
176 lines
5.5 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\Aisle;
|
|
use App\Models\Brand;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class BrandController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
|
|
/**
|
|
* 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): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Brand> $brandModels */
|
|
$brandModels = Brand::query()->with('aisle');
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->input('search');
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$brandModels,
|
|
$search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
Str::ucfirst(trans('validation.attributes.name')),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($brandModels);
|
|
|
|
/** Custom pagination */
|
|
$brandModels = $this->paginate($brandModels);
|
|
|
|
return view('bo.pages.brands.index', compact('brandModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Brand $brand): \Illuminate\Contracts\View\View
|
|
{
|
|
$aisleModels = Aisle::all();
|
|
|
|
return view('bo.pages.brands.create', [
|
|
'brandModel' => $brand,
|
|
'aisleModels' => $aisleModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\StoreBrandRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreBrandRequest $request): \Illuminate\Http\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', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.brand'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.brand'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Brand $brand): \Illuminate\Contracts\View\View
|
|
{
|
|
$aisleModels = Aisle::all();
|
|
|
|
return view('bo.pages.brands.edit', [
|
|
'brandModel' => $brand,
|
|
'aisleModels' => $aisleModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $brand) {
|
|
$brand->fill($request->validated());
|
|
|
|
if ($brand->saveOrFail()) {
|
|
return redirect()->route('bo.brands.edit', $brand)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.brand'))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.brands.edit', $brand)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.brand'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Brand $brand): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($brand) {
|
|
if ($brand->deleteOrFail()) {
|
|
return redirect()->route('bo.brands.index')
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.brand'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.brand'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Brand $brand): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($brand->replicate());
|
|
}
|
|
}
|