Question

I'm currently working on a project with a search function. I'm getting some data from my database. I bind this data through my controller. And fill my dropdownlist with it, including a default select item. Is it possible to set the values from the select to the data I retrieve from my database?

// Browser

<select id="game_kind" name="game_kind">
      <option value="" selected="selected">Make your choice..</option>
      <option value="1">puzzle</option>
      <option value="4">dice game</option>
      <option value="5">card game</option>
      <option value="6">board game</option>
 </select>

// Controller

    public function Filter()
    {
        $game_kinds = GameKind::where('game_kind_name_en','!=','')->lists('game_kind_name_en');

        return View::make('web.spelzoeken', compact('game_kinds');
    }

// View

    {{ Form::open(['route' => 'web.search.post']), PHP_EOL }}
       {{ Form::label('game_kind',Lang::get('web-zoeken.search-game'))}}
       {{ Form::select('game_kind', ['' => Lang::get('web-zoeken.search-option')] + $game_kinds)}}
       {{ Form::submit(Lang::get('web-zoeken.search-button'), ['class' => 'button tiny']), PHP_EOL }}
    {{ Form::close(), PHP_EOL }}

// Result I really want

<select id="game_kind" name="game_kind">
      <option value="" selected="selected">Make your choice..</option>
      <option value="puzzle">puzzle</option>
      <option value="dice game">dice game</option>
      <option value="card game">card game</option>
      <option value="board game">board game</option>
 </select>

Thanks in advance!

Was it helpful?

Solution

Change this:

$game_kinds = GameKind::where('game_kind_name_en','!=','')
    ->lists('game_kind_name_en');

into this:

// SELECT 'field' AS 'alias' ...
$game_kinds = GameKind::select('game_kind_name_en', 'game_kind_name_en as game_kind_name_en_key') 
    ->where('game_kind_name_en','!=','')
    ->lists('game_kind_name_en', 'game_kind_name_en_key');

From the docs:

Specifying A Select Clause

$users = DB::table('users')->select('name as user_name')->get();

or ...->lists(...) in your case

And

Retrieving A List Of Column Values

$roles = DB::table('roles')->lists('title');

This method will return an array of role titles. You may also specify a custom key column for the returned array:

$roles = DB::table('roles')->lists('title', 'name');


BTW

- Don't use spaces in HTML-attribute names.

Instead of doing:

value="dice game"

Do like:

value="dice_game".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top