middleware('auth'); $this->middleware('role:ADMIN'); } public function index() { return view('words.index', [ 'words' => Word::latest('id')->paginate(10), ]); } /** * Store a new Word in the database. */ public function store(StoreWordRequest $request): RedirectResponse { $word = new Word; $word->name = $request->name; $word->translation = $request->translation; $word->save(); return redirect('/words')->withSuccess('The Italian word and its translation have been successfully saved.'); } /** * Update an existing Word in the database. */ public function edit($id) { $data = Word::find($id); $id = $data->id; $name = $data->name; $translation = $data->translation; //return $translation; return view("words.partials.modify-word-form", compact('id', "name", "translation")); } public function insert() { return view("words.partials.insert-word-form"); } /** * Update an existing Word in the database. */ public function update(UpdateWordRequest $request, $id): RedirectResponse { $data = Word::find($id); $data->translation = $request->input('translation'); $data->save(); // Update the unique value $data->name = $request->input('name'); // Check for uniqueness $isUnique = Word::where('name', $data->name) ->where('id', '!=', $id) // Exclude the current record from the uniqueness check ->doesntExist(); if ($isUnique) { // Save the updated record $data->translation = $request->input('translation'); $data->save(); // Redirect to a success route or return a success message return redirect('/words')->withSuccess('The Italian word and its translation has been successfully changed.'); } else { // Unique constraint violated return back()->withInput()->withErrors(['name' => 'Error: words already exists']); } } /** * Delete an existing Word in the database. */ public function delete($id): RedirectResponse { Word::destroy($id); return redirect('/words')->withSuccess('The Italian word and its translation has been successfully deleted.'); } }