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