find($gameId); if( $game ){ $this->gameId = $game->id; $this->gameName = $game->name; $this->platformName = $game->platform->name; $this->genreName = $game->genre->name; $this->search = $game->name; $this->newGame = false; return; } } if( $newGameTitle || $newGamePlatform || $newGameGenre ){ $this->newGame = true; $this->gameName = $newGameTitle; $this->gamePlatformId = is_numeric($newGamePlatform) ? (int) $newGamePlatform : null; $this->gameGenreId = is_numeric($newGameGenre) ? (int) $newGameGenre : null; } } /** * If we update search bar. * @return void */ public function updatedSearch(): void { if( $this->gameId ){ $this->gameId = null; $this->gameName = null; $this->platformName = null; $this->genreName = null; } $this->dropdown = strlen($this->search) >= self::REQUIRED_CHARS; } /** * Select an existent game. * * @param int $id * @param string $name * * @return void */ public function selectGame( int $id, string $name ): void { $game = Game::with(['platform','genre'])->find($id); if( $game ){ $this->gameId = $game->id; $this->gameName = $game->name; $this->platformName = $game->platform->name; $this->genreName = $game->genre->name; $this->search = $game->name; $this->dropdown = false; $this->dispatch( 'game-selected', id: $id ); // Send an event to the JS part. } } /** * Clear existent game selection. * @return void */ public function clearGame(): void { $this->gameId = null; $this->gameName = null; $this->platformName = null; $this->genreName = null; $this->search = ''; $this->dispatch( 'game-selected', id: null ); // Send an event to the JS part. } /** * Switch mode. * @return void */ public function switchNewGame(): void { $this->clearGame(); $this->newGame = !$this->newGame; } public function render(): View { $games = collect(); // Need to search games for dropdown. if( $this->dropdown && strlen($this->search) >= self::REQUIRED_CHARS && $this->newGame === false ){ $games = Game::with(['platform','genre']) ->where('name', 'like', '%'.$this->search.'%') ->orderBy('name') ->limit(20) ->get(); } $data = [ 'games' => $games, 'required_chars' => self::REQUIRED_CHARS ]; if( $this->newGame === true ){ // If we want a new game, get platforms and genres. $data['platforms'] = Platform::orderBy('name')->get(); $data['genres'] = Genre::orderBy('name')->get(); } $data['hasOldNewGame'] = old('new-game-title') || old('new-game-platform') || old('new-game-genre'); return view('livewire.game-selector', $data ); } }