'required|string|max:255', 'selectedCategory' => 'required|array', ]; public function mount() { $this->artistCategory = ArtistCategory::all(); } public function openCreateArtistModal() { $this->resetFields(); $this->showCreateModal = true; } public function openEditArtistModal($id) { $artist = Artist::findOrFail($id); $this->artistId = $artist->id; $this->name = $artist->name; $this->selectedCategory = $artist->category_id; $this->showCreateModal = true; } public function save() { $this->validate(); if ($this->artistId) { $role = Artist::findOrFail($this->artistId); $role->update([ 'category_id' => $this->selectedCategory, 'name' => $this->name, ]); session()->flash('message', '歌手已更新'); } else { $role = Artist::create([ 'category_id' => $this->selectedCategory, 'name' => $this->name, ]); session()->flash('message', '歌手已新增'); } $this->resetFields(); $this->showCreateModal = false; } public function deleteArtist($id) { Artist::findOrFail($id)->delete(); session()->flash('message', '歌手已刪除'); } public function resetFields() { $this->name = ''; $this->selectedCategory = []; $this->artistId = null; } public function render() { return view('livewire.admin.artist-form'); } }