Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
| BrandController | n/a |
0 / 0 |
n/a |
0 / 0 |
9 | n/a |
0 / 0 |
|||
| index | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| create | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| store | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| edit | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| update | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| jsonPaginate | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Requests\Brands\StoreBrandRequest; |
| 7 | use App\Http\Requests\Brands\UpdateBrandRequest; |
| 8 | use App\Models\Brand; |
| 9 | use Illuminate\Database\Eloquent\Builder; |
| 10 | use Illuminate\Http\Request; |
| 11 | use Illuminate\Support\Facades\DB; |
| 12 | use Illuminate\Support\Str; |
| 13 | |
| 14 | /** |
| 15 | * @codeCoverageIgnore |
| 16 | */ |
| 17 | class BrandController extends Controller |
| 18 | { |
| 19 | /** |
| 20 | * Display a listing of the resource. |
| 21 | * |
| 22 | * @param \Illuminate\Http\Request $request |
| 23 | * @return \Illuminate\Http\Response |
| 24 | */ |
| 25 | public function index(Request $request) |
| 26 | { |
| 27 | /** @var \Illuminate\Database\Eloquent\Builder $query */ |
| 28 | $query = Brand::query(); |
| 29 | |
| 30 | if ($request->search) { |
| 31 | $this->searchQuery( |
| 32 | $query, |
| 33 | $request->search, |
| 34 | null, |
| 35 | 'name', |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | $this->sortQuery($query); |
| 40 | $searchFields = trans('Nom'); |
| 41 | |
| 42 | $query->withCount(['customerInfos', 'customerInfos as customer_infos_optin_count' => function (Builder $q) { |
| 43 | $q->where('opt_in', true); |
| 44 | }]); |
| 45 | |
| 46 | $brandModels = $query->paginate(12); |
| 47 | |
| 48 | return view('brands.index', compact('brandModels', 'searchFields')); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Show the form for creating a new resource. |
| 53 | * |
| 54 | * @param \App\Models\Brand $brands |
| 55 | * @return \Illuminate\Http\Response |
| 56 | */ |
| 57 | public function create(Brand $brands) |
| 58 | { |
| 59 | return view('brands.create', [ |
| 60 | 'brandModel' => $brands |
| 61 | ]); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Store a newly created resource in storage. |
| 66 | * |
| 67 | * @param \App\Http\Requests\Brands\StoreBrandRequest $request |
| 68 | * @return \Illuminate\Http\Response |
| 69 | */ |
| 70 | public function store(StoreBrandRequest $request) |
| 71 | { |
| 72 | return DB::transaction(function () use ($request) { |
| 73 | $brand = new Brand(); |
| 74 | $brand->fill($request->validated()); |
| 75 | if ($brand->saveOrFail()) { |
| 76 | return redirect() |
| 77 | ->route('brands.edit', $brand) |
| 78 | ->with('success', trans( |
| 79 | ':model a été créé.', |
| 80 | ['model' => Str::singular(trans('models.classes.brands'))] |
| 81 | )); |
| 82 | } |
| 83 | return redirect() |
| 84 | ->back() |
| 85 | ->with('error', trans( |
| 86 | ':model n\'a pas été créé.', |
| 87 | ['model' => Str::singular(trans('models.classes.brands'))] |
| 88 | )); |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Show the form for editing the specified resource. |
| 94 | * |
| 95 | * @param \App\Models\Brand $brands |
| 96 | * @return \Illuminate\Http\Response |
| 97 | */ |
| 98 | public function edit(Brand $brands) |
| 99 | { |
| 100 | return view('brands.edit', [ |
| 101 | 'brandModel' => $brands |
| 102 | ]); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Update a spacific resource in storage. |
| 107 | * |
| 108 | * @param \App\Http\Requests\Brands\UpdateBrandRequest $request |
| 109 | * @param \App\Models\Brand $brand |
| 110 | * @return \Illuminate\Http\Response |
| 111 | */ |
| 112 | public function update(UpdateBrandRequest $request, Brand $brand) |
| 113 | { |
| 114 | return DB::transaction(function () use ($request, $brand) { |
| 115 | $brand->fill($request->validated()); |
| 116 | if ($brand->saveOrFail()) { |
| 117 | return redirect() |
| 118 | ->route('brands.edit', $brand) |
| 119 | ->with('success', trans( |
| 120 | ':model a été mis à jour.', |
| 121 | ['model' => Str::singular(trans('models.classes.brands'))] |
| 122 | )); |
| 123 | } |
| 124 | return redirect() |
| 125 | ->back() |
| 126 | ->with('error', trans( |
| 127 | ':model n\'a pas été mis à jour.', |
| 128 | ['model' => Str::singular(trans('models.classes.brands'))] |
| 129 | )); |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Store a newly created resource in storage. |
| 135 | * |
| 136 | * @return \Illuminate\Http\Response |
| 137 | */ |
| 138 | public function jsonPaginate() |
| 139 | { |
| 140 | return DB::transaction(function () { |
| 141 | /** @var \App\Models\Tag */ |
| 142 | $brandModels = Brand::paginate(12); |
| 143 | return \response()->json($brandModels ?? 500); |
| 144 | }); |
| 145 | } |
| 146 | } |