brands = $brand; } /** * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function index() { $this->authorize('view', 'brands'); $brands = $this->brands->orderBy('position')->paginate(20); return view('dashboard.brands.index', compact('brands')); } /** * @param UpdateRequest $request * @param Brand $brand * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function update(UpdateRequest $request, Brand $brand) { if ($request->isMethod('get')) { $this->authorize('update', 'brands'); return view('dashboard.brands.update', compact('brand')); } if ($request->hasFile('image')) { Storage::delete($brand->image); $path = $this->storeImageToS3($request->file('image')); } else { $path = $brand->image; } $this->dispatchSync(UpdateJob::fromRequest($brand, $request, $path)); $this->info(trans('admin.messages.updated')); return redirect()->route('dashboard.brands'); } /** * @param StoreRequest $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function store(StoreRequest $request) { if ($request->isMethod('get')) { $this->authorize('create', 'brands'); return view('dashboard.brands.store'); } if ($request->hasFile('image')) { $path = $this->storeImageToS3($request->file('image')); } $this->dispatchSync(StoreJob::fromRequest($request, $path)); $this->info(trans('admin.messages.created')); return redirect()->route('dashboard.brands'); } /** * @param Brand $brand * @return \Illuminate\Http\RedirectResponse * @throws \Exception */ public function delete(Brand $brand) { $this->authorize('delete', 'brands'); Storage::delete($brand->image); Product::where('brand_id', $brand->id)->withTrashed()->update([ 'brand_id' => null ]); $brand->delete(); $this->info(trans('admin.messages.deleted')); return redirect()->back(); } private function storeImageToS3($image): string { $tempPath = $image->store('temp', 'public'); $resizer = new ImageResize(); return $resizer->resize($tempPath, 322, 'brands', true); } }