178 lines
5.1 KiB
PHP
178 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Brands\GetBrandUrlsRequest;
|
|
use App\Http\Requests\Bo\Brands\StoreBrandRequest;
|
|
use App\Http\Requests\Bo\Brands\UpdateBrandRequest;
|
|
use App\Models\Brand;
|
|
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\Factory|\Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $brands */
|
|
$brands = Brand::query();
|
|
|
|
if ($request->search) {
|
|
$this->searchQuery(
|
|
$brands,
|
|
$request->search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
|
|
$this->sortQuery($brands);
|
|
$searchFields = trans('Nom');
|
|
|
|
$brands = $this->paginate($brands);
|
|
|
|
return view('bo.pages.brands.index', compact('brands', 'searchFields'));
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
*/
|
|
public function create()
|
|
{
|
|
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)
|
|
{
|
|
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(
|
|
':model a été créé',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':model n\'a pas été créé',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Brand $brand)
|
|
{
|
|
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)
|
|
{
|
|
return DB::transaction(function () use ($request, $brand) {
|
|
$brand->fill($request->validated());
|
|
if ($brand->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.edit', $brand)
|
|
->with('success', trans(
|
|
':model a été mis à jour',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':model n\'a pas été mis à jour',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Brand $brand)
|
|
{
|
|
if ($brand->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.index')
|
|
->with('warning', trans(
|
|
':model a bien été supprimée',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':model n\'a pas été supprimée',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Get urls from a given brand.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\GetBrandUrlsRequest $request
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getUrls(GetBrandUrlsRequest $request, Brand $brand)
|
|
{
|
|
if (!$request->wantsJson()) {
|
|
return abort(404);
|
|
}
|
|
|
|
return response()
|
|
->json($brand->urls);
|
|
}
|
|
}
|