Phil Nind
Phil Nind

Reputation: 341

Laravel - Form::select show name as Option Value

I have a database of Leagues and I need to have a drop down list inside a form of the different leagues...

I have got this working but the Option Value is displaying as the ID number and I need it to display as the league_name field...

Controller

public function getEdit($id = null)
{

    try {
        // Get the group information
        $tip = Tips::find($id);

        $league = Leagues::lists('league_name', 'id');

    } catch (TipNotFoundException $e) {
        // Redirect to the groups management page
        return Redirect::route('tips')->with('error', Lang::get('tips/message.group_not_found', compact('id')));
    }

    // Show the page
    return View('admin/tips/edit', compact('tip','league'));
}

View Blade

<div class="form-group">
 <label for="league" class="col-sm-3 control-label">League</label>
   <div class="col-sm-9">
      {!! Form::select('league', $league, Input::old('league'), ['class' => 'form-control required', 'id' => 'league', 'placeholder' => 'Select League']) !!}
   </div>
</div>

It is outputting as...

<select class="form-control required" id="league" name="league">
 <option selected="selected" value="">Select League</option>
 <option value="1">FIFA World Cup</option>
 <option value="2">FIFA Women's World Cup</option>
 <option value="3">FIFA Confederations Cup</option>
 <option value="4">Summer Olympic Games (FIFA unofficial)</option>

I want it to output as...

<select class="form-control required" id="league" name="league">
 <option selected="selected" value="">Select League</option>
 <option value="FIFA World Cup">FIFA World Cup</option>
 <option value="FIFA Women's World Cup">FIFA Women's World Cup</option>
 <option value="FIFA Confederations Cup">FIFA Confederations Cup</option>
 <option value="Summer Olympic Games (FIFA unofficial)">Summer Olympic Games (FIFA unofficial)</option>

Upvotes: 1

Views: 4245

Answers (1)

Ben Swinburne
Ben Swinburne

Reputation: 26477

The lists method builds an array with the keys and values specified.

array lists ( string $values_column [, string $keys_column = null ] );

In your example you're explicitly setting the keys as the id column of each row which is why you're getting numeric values.

Simply changing the call to lists to use league_name as both the key and the value will give you the result you want.

$league = Leagues::lists('league_name', 'league_name');

With regards to your comment/question about retrieving an existing selection for this field:

Utilising the second parameter of Input::old() (or it's shorter form helper old() you can specify a value if previously submit data doesn't exist.

old('league', $value_if_old_isnt_set);

This will preselect the field with $value_if_old_isnt_set on page load, then subsequently populate it with the value submit with the form.

Answered in the comments but put here so it can be accepted and it'll disappear from the unanswered lists

Upvotes: 1

Related Questions