Search code examples
phpselectlaravel-5laravelcollective

Laravel 5.2 - Passing a value and text to select box


In my controller I am passing a multiple array to the view. The arrays that I am passing look like this:

$charts['names'] = ['Artikler lest', 'Antall kommentarer', 'Antall "bli med"', 'Tid på døgnet'];
$charts['values'] = ['Article', 'Comment', 'Thumb', 'View'];

return view('admin.charts.show',
      compact(
        'charts',
      )
    );
  }

And in my view I have a select box that looks like this:

 <div class="large-4 columns end">
     {!! Form::select('velg', $charts['names'], null, ['id' => 'timelines']) !!}
 </div>

With this I am passing just text to the select box, I would also like to pass a values so that they are connnected like this:

<option value="Article">Artikler lest</option>
<option value="Comment">Antall kommentarer</option>
<option value="Thumb">Antall "bli med"</option>
<option value="View">Tid på døgnet</option>

How would I do such thing with Laravel collective?


Solution

  • Just pass through an array with the keys as the option value:

    $charts = [
        'Article' => 'Artikler lest', 
        'Comment' => 'Antall kommentarer', 
        'Thumb' => 'Antall "bli med"', 
        'View' => 'Tid på døgnet'
    ]; 
    

    The form builder will use the key for the value of the option. https://laravelcollective.com/docs/5.2/html#drop-down-lists