2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2023-06-15 12:24:40 +02:00
|
|
|
use App\Http\Requests\Brands\StoreBrandRequest;
|
|
|
|
|
use App\Http\Requests\Brands\UpdateBrandRequest;
|
2020-12-01 17:02:10 +01:00
|
|
|
use App\Models\Brand;
|
2023-06-23 13:29:03 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-06-13 09:11:18 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Str;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
2020-12-01 17:02:10 +01:00
|
|
|
class BrandController extends Controller
|
|
|
|
|
{
|
2023-06-13 09:11:18 +02:00
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
2023-06-15 13:18:48 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder $query */
|
|
|
|
|
$query = Brand::query();
|
2023-06-13 09:11:18 +02:00
|
|
|
|
|
|
|
|
if ($request->search) {
|
|
|
|
|
$this->searchQuery(
|
2023-06-15 13:18:48 +02:00
|
|
|
$query,
|
2023-06-13 09:11:18 +02:00
|
|
|
$request->search,
|
|
|
|
|
null,
|
|
|
|
|
'name',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 13:18:48 +02:00
|
|
|
$this->sortQuery($query);
|
2023-06-13 09:11:18 +02:00
|
|
|
$searchFields = trans('Nom');
|
|
|
|
|
|
2023-06-23 13:29:03 +02:00
|
|
|
$query->withCount(['customerInfos', 'customerInfos as customer_infos_optin_count' => function (Builder $q) {
|
|
|
|
|
$q->where('opt_in', true);
|
|
|
|
|
}]);
|
|
|
|
|
|
2023-06-15 13:18:48 +02:00
|
|
|
$brandModels = $query->paginate(12);
|
2023-06-13 09:11:18 +02:00
|
|
|
|
|
|
|
|
return view('brands.index', compact('brandModels', 'searchFields'));
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 09:11:18 +02:00
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Brand $brands
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function create(Brand $brands)
|
|
|
|
|
{
|
|
|
|
|
return view('brands.create', [
|
|
|
|
|
'brandModel' => $brands
|
|
|
|
|
]);
|
|
|
|
|
}
|
2021-09-08 16:43:44 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
2023-06-13 09:11:18 +02:00
|
|
|
* Store a newly created resource in storage.
|
2021-12-19 15:57:24 +01:00
|
|
|
*
|
2023-06-13 09:11:18 +02:00
|
|
|
* @param \App\Http\Requests\Brands\StoreBrandRequest $request
|
|
|
|
|
* @return \Illuminate\Http\Response
|
2021-12-19 15:57:24 +01:00
|
|
|
*/
|
2023-06-13 09:11:18 +02:00
|
|
|
public function store(StoreBrandRequest $request)
|
|
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request) {
|
|
|
|
|
$brand = new Brand();
|
|
|
|
|
$brand->fill($request->validated());
|
|
|
|
|
if ($brand->saveOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('brands.edit', $brand)
|
|
|
|
|
->with('success', trans(
|
2023-06-15 12:24:40 +02:00
|
|
|
':model a été créé.',
|
2023-06-13 09:11:18 +02:00
|
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
2023-06-15 12:24:40 +02:00
|
|
|
':model n\'a pas été créé.',
|
2023-06-13 09:11:18 +02:00
|
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-08 16:43:44 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
2023-06-13 09:11:18 +02:00
|
|
|
* Show the form for editing the specified resource.
|
2021-12-19 15:57:24 +01:00
|
|
|
*
|
2023-06-13 09:11:18 +02:00
|
|
|
* @param \App\Models\Brand $brands
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2023-06-13 09:11:18 +02:00
|
|
|
public function edit(Brand $brands)
|
2021-02-28 12:04:49 +01:00
|
|
|
{
|
2023-06-13 09:11:18 +02:00
|
|
|
return view('brands.edit', [
|
|
|
|
|
'brandModel' => $brands
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update a spacific resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Brands\UpdateBrandRequest $request
|
2023-06-15 12:24:40 +02:00
|
|
|
* @param \App\Models\Brand $brand
|
2023-06-13 09:11:18 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2023-06-15 12:24:40 +02:00
|
|
|
public function update(UpdateBrandRequest $request, Brand $brand)
|
2023-06-13 09:11:18 +02:00
|
|
|
{
|
2023-06-15 12:24:40 +02:00
|
|
|
return DB::transaction(function () use ($request, $brand) {
|
2023-06-13 09:11:18 +02:00
|
|
|
$brand->fill($request->validated());
|
|
|
|
|
if ($brand->saveOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('brands.edit', $brand)
|
|
|
|
|
->with('success', trans(
|
2023-06-15 12:24:40 +02:00
|
|
|
':model a été mis à jour.',
|
2023-06-13 09:11:18 +02:00
|
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
2023-06-15 12:24:40 +02:00
|
|
|
':model n\'a pas été mis à jour.',
|
2023-06-13 09:11:18 +02:00
|
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
|
|
|
));
|
|
|
|
|
});
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
2023-06-23 13:29:45 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function jsonPaginate()
|
|
|
|
|
{
|
|
|
|
|
return DB::transaction(function () {
|
|
|
|
|
/** @var \App\Models\Tag */
|
|
|
|
|
$brandModels = Brand::paginate(12);
|
|
|
|
|
return \response()->json($brandModels ?? 500);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|