Edit File: GenreEdit.php
<?php namespace App\Livewire\Admin\Genre; use App\Models\Genre; use App\Models\History; use Livewire\Component; use Illuminate\Support\Str; use Illuminate\Validation\Rule; use Livewire\Attributes\Layout; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; use Illuminate\Validation\Rules\Unique; class GenreEdit extends Component { public $genre; public string $name; protected function rules() { return [ 'name' => [ 'required', 'string', 'max:100', Rule::unique("genres", "name")->ignore($this->genre->id), ], ]; } public function mount($id) { $this->genre = Genre::find($id); if ($this->genre) { $this->name = $this->genre->name; } else { abort(404, "Genre not found"); } } public function update() { $this->validate(); $this->genre->name = $this->name; $this->genre->slug = Str::slug($this->name); $this->genre->save(); History::create([ 'name' => Auth::user()->name, 'page' => 'Genre', 'title' => Auth::user()->name . ' Mengupdate Sosmed Baru ' . $this->genre->name, 'status' => 'info', ]); Session::flash('status', 'success'); Session::flash('message', 'Berhasil Mengupdate Genre ' . $this->genre->name); return redirect()->route("admin.genre.index"); } #[Layout('layouts.admin')] public function render() { return view('livewire.admin.genre.genre-edit'); } }
Back