'unset', 'name' =>'', ]; public $selectedCategory = []; // 表單中選到的權限 public function mount() { $this->categoryOptions = collect(ArtistCategory::cases())->map(fn ($category) => [ 'name' => $category->labels(), 'value' => $category->value, ])->toArray(); } public function openCreateArtistModal() { $this->resetFields(); $this->showCreateModal = true; } public function openEditArtistModal($id) { $artist = Artist::findOrFail($id); $this->artistId = $artist->id; $this->fields = $artist->only(array_keys($this->fields)); $this->showCreateModal = true; } public function save() { if ($this->artistId) { $artist = Artist::findOrFail($this->artistId); $artist->update($this->fields); session()->flash('message', '歌手已更新'); } else { $artist = Artist::create($this->fields); session()->flash('message', '歌手已新增'); } $this->resetFields(); $this->showCreateModal = false; $this->dispatch('pg:eventRefresh-artist-table'); } public function deleteArtist($id) { Artist::findOrFail($id)->delete(); session()->flash('message', '歌手已刪除'); } public function resetFields() { foreach ($this->fields as $key => $value) { if ($key == 'category') { $this->fields[$key] = 'unset'; } else { $this->fields[$key] = ''; } } $this->artistId = null; } public function render() { return view('livewire.admin.artist-form'); } }