88 lines
2.1 KiB
PHP
88 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use Livewire\Component;
|
|
|
|
use App\Models\Artist;
|
|
use App\Models\ArtistCategory;
|
|
|
|
class ArtistForm extends Component
|
|
{
|
|
protected $listeners = ['openCreateArtistModal','openEditArtistModal', 'deleteArtist'];
|
|
|
|
public bool $showCreateModal = false;
|
|
public ?int $artistId = null;
|
|
public $name;
|
|
public $artistCategory = []; // 所有類別清單
|
|
public $selectedCategory = []; // 表單中選到的權限
|
|
|
|
protected $rules = [
|
|
'name' => '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');
|
|
}
|
|
}
|