search) { $this->searchQuery( $urls, $request->search, null, 'key', 'value' ); } $this->sortQuery($urls); $searchFields = trans('Clé, Redirection'); $urls = $urls->paginate(10); return view('bo.pages.urls.index', compact('urls', 'searchFields')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('bo.pages.urls.create', [ 'brands' => Brand::all() ]); } /** * Store a newly created resource in storage. * * @param StoreUrlRequest $request * @return \Illuminate\Http\Response */ public function store(StoreUrlRequest $request) { return DB::transaction(function () use ($request) { $url = new Url(); $url->fill($request->validated()); $url->saveOrFail(); // Reassign the logo in image settings to save only the path. // Do it after the first saveOrFail to get the real logo path, not tmp. $settings = json_decode($url->image_settings); $settings->image = $url->logo; $url->image_settings = $settings; if ($url->saveOrFail()) { return redirect() ->route('bo.urls.edit', $url) ->with('success', trans( ':model a été créé', ['model' => Str::singular(trans('models.classes.urls'))] )); } return redirect() ->back() ->with('error', trans( ':model n\'a pas été créé', ['model' => Str::singular(trans('models.classes.urls'))] )); }); } /** * Show the form for editing the specified resource. * * @param \App\Models\Url $url * @return \Illuminate\Http\Response */ public function edit(Url $url) { return view('bo.pages.urls.edit', [ 'urlModel' => $url, 'brands' => Brand::all() ]); } /** * Update the specified resource in storage. * * @param UpdateUrlRequest $request * @param Url $url * @return \Illuminate\Http\Response */ public function update(UpdateUrlRequest $request, Url $url) { return DB::transaction(function () use ($request, $url) { $oldUrl = $url->value; $url->fill($request->validated()); $url->saveOrFail(); if ($oldUrl !== $url->value) { $this->saveUrlHistory($url, $oldUrl); } // Reassign the logo in image settings to save only the path. // Do it after the first saveOrFail to get the real logo path, not tmp. $settings = json_decode($url->image_settings); $settings->image = $url->logo; $url->image_settings = $settings; if ($url->saveOrFail()) { return redirect() ->route('bo.urls.edit', $url) ->with('success', trans( ':model a été mis à jour', ['model' => Str::singular(trans('models.classes.urls'))] )); } return redirect() ->back() ->with('error', trans( ':model n\'a pas été mis à jour', ['model' => Str::singular(trans('models.classes.urls'))] )); }); } /** * Remove the specified resource from storage. * * @param Url $url * @return \Illuminate\Http\Response */ public function destroy(Url $url) { foreach ($url->history as $history) { $history->deleteOrFail(); } foreach ($url->stats as $stats) { $stats->deleteOrFail(); } $url->deleteOrFail(); return redirect()->route('bo.urls.index') ->with(['warning' => 'L\'url pour la clé ' . $url->key . ' a bien été supprimée.']); } /** * Save url history when updating url. * @param Url $url * @param string $oldValue * @return UrlHistory */ private function saveUrlHistory(Url $url, string $oldValue) { $history = new UrlHistory(); $history->value = $oldValue; $history->start_date = $url->updated_at; $history->end_date = now(); $history->url_id = $url->id; $history->saveOrFail(); return $history; } }