diff --git a/app/Livewire/Layout/Navigation.php b/app/Livewire/Layout/Navigation.php index b2ded47..584bd8a 100644 --- a/app/Livewire/Layout/Navigation.php +++ b/app/Livewire/Layout/Navigation.php @@ -12,7 +12,6 @@ class Navigation extends Component ['name' => '首頁', 'route' => 'welcome'], ['name' => '新歌快報', 'route' => 'new-songs'], ['name' => '熱門排行', 'route' => 'top-ranking'], - ['name' => '歌星查詢', 'route' => 'search-singer'], ['name' => '歌名查詢', 'route' => 'search-song'], ['name' => '已點歌曲', 'route' => 'clicked-song'], ['name' => '聲音控制', 'route' => 'sound-control'], diff --git a/app/Livewire/Pages/SearchSong.php b/app/Livewire/Pages/SearchSong.php index bc234ae..9c4e7fd 100644 --- a/app/Livewire/Pages/SearchSong.php +++ b/app/Livewire/Pages/SearchSong.php @@ -3,6 +3,7 @@ namespace App\Livewire\Pages; use Livewire\Component; +use App\Models\Artist; use App\Models\Song; class SearchSong extends Component @@ -10,6 +11,9 @@ class SearchSong extends Component public string $search = ''; public $searchCategory = ''; public $selectedLanguage = '國語'; // 預設語言 + public $selectedArtists = []; // 儲存已選的歌手 id + public $artistOptions = []; // API 回傳的搜尋結果 + public string $artistSearch = ''; // 即時搜尋文字 /** * 初始化(接收外部傳入的分類參數) @@ -26,10 +30,35 @@ class SearchSong extends Component { $this->selectedLanguage = $lang; } - //public function updatedSearch() - //{ - // dd($this->search); - //} + /** + * 搜尋歌手(透過 API) + */ + public function updatedArtistSearch() + { + $search = $this->artistSearch; + + if ($search) { + // 即時搜尋歌手,從資料庫查詢 + $this->artistOptions = \App\Models\Artist::query() + ->where('name', 'like', "{$search}%") + ->limit(100) + ->get(['id', 'name']) + ->toArray(); + } else { + $this->artistOptions = []; + } + } + public function addArtist($id, $name) + { + $this->selectedArtists[$id] = $name; + $this->artistSearch = ''; // 清空搜尋框 + $this->artistOptions = []; + } + + public function removeArtist($id) + { + unset($this->selectedArtists[$id]); + } /** * 點歌 @@ -50,22 +79,14 @@ class SearchSong extends Component protected function loadSongs() { return Song::query() - ->when($this->search !== null && $this->search !== '', function ($q) { - $q->where('name', 'like', "{$this->search}%"); - }) - ->when($this->selectedLanguage, function ($q) { - $q->where('language_type', $this->selectedLanguage); - }) - ->when($this->searchCategory === 'New', function ($q) { - $q->orderByDesc('created_at'); - }) - ->when($this->searchCategory === 'Hot', function ($q) { - $q->orderByDesc('song_counts'); - }, function ($q) { - $q->orderBy('name'); - }) - ->take($this->search ? 100 : 200) // 搜尋就多拿點,不搜尋就少拿 - ->get(); + ->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%")) + ->when($this->selectedLanguage, fn($q) => $q->where('language_type', $this->selectedLanguage)) + ->when($this->selectedArtists, fn ($q) => $q->whereHas('artists', fn ($q2) => $q2->whereIn('id', array_keys($this->selectedArtists)))) + ->when($this->searchCategory === 'New', fn($q) => $q->orderByDesc('created_at')) + ->when($this->searchCategory === 'Hot', fn($q) => $q->orderByDesc('song_counts'), fn($q) => $q->orderByDesc('id')) + //->take($this->search || $this->artistSearch ? 100 : 1000) + ->take(1000) + ->get(); } /** diff --git a/resources/views/livewire/pages/search-song.blade.php b/resources/views/livewire/pages/search-song.blade.php index 837cbc5..123ccfa 100644 --- a/resources/views/livewire/pages/search-song.blade.php +++ b/resources/views/livewire/pages/search-song.blade.php @@ -1,5 +1,39 @@
+
+ + + @if(!empty($artistOptions)) +
+ @foreach($artistOptions as $artist) +
+ {{ $artist['name'] }} + @if(in_array($artist['id'], $selectedArtists)) + + @endif +
+ @endforeach +
+ @endif + + + @if($selectedArtists) +
+ @foreach($selectedArtists as $artistId => $artistName) + + {{ $artistName }} + + + @endforeach +
+ @endif +
{{-- 搜尋框 --}}
-