Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jhutto's avatar

Creating simple dropdown from database - Laravel Collective Form

I'm new to laravel and just trying to stay up to speed with the different versions. Using Laravel 5.6

I have created blade form and have been able to create my dropdown from the database,, but I keep running into forum questions showing selects using the Collective Form sytle. But I can't find anything in the documentation about the arguments for the {{ Form::Select ( arg1, arg2, arg3, arg4)}} ....... or how to create it from a database table.

So, I need help in writing this {{Form::Select }} statement that does the same thing as below.

Create Form ... Select looping through and creating the dropdown. Works good. Controller just pulls the list from database and passes it to the form as $instructors

<div class="form-group col-md-6">
                                                <label for="title">Instructor:</label>
                                                    <select name="Instructorid" id="Instructorid" class="form-control" style="width:350px;margin-left:50px;">
                                                        <option value="">--- Select Instuctor ---</option>
                                                        @foreach ($instructors as $instructor)
                                                            <option value="{{ $instructor->id }}">{{ $instructor->FirstName }} {{ $instructor->LastName }}"</option>">
                                                        @endforeach
                                                    </select>
                                                </label>
 </div>
``


How do I create the same loop in the {{ Form::select('Instructors', $instructors, ....) }} 
format?





0 likes
3 replies
Cronix's avatar

$instructors needs to be an array of just key/value pairs. Then it will create the dropdown with options created from that array

<option value="$key">$value</option>

$options = [
    '24' => 'Product 1',
    '32' => 'Product 2',
    '54' => 'Product 3'
];

$selected = 24;

{!! Form::select('test', $options, $selected) !!}
<select name="test">
    <option value="24" selected>Product 1</option>
    <option value="32">Product 2</option>
    <option value="54">Product 3</option>
</select>

https://laravelcollective.com/docs/5.4/html#drop-down-lists

1 like
Cronix's avatar
Cronix
Best Answer
Level 67

A nice helper laravel has for collections is pluck(). You can use that to easily create the array from the collection ($instructors) without having to manually iterate over the collection to create the array to use in the dropdown.

Something like

$options = $instructors->pluck('FirstName', 'id')->toArray();

https://laravel.com/docs/5.6/collections#method-pluck

1 like
jhutto's avatar

That helped... Got some additional help and finalized it with this.... Thanks Croniz..

In Controller:

$instructors = DB::table('instructors')
            ->where('instructors.Viewable','=', 1)
            ->select(DB::raw('concat (FirstName," ",LastName) as FullName, id'));
            

$instructorOptions = array('' => 'Select Instructor') + $instructors->pluck('FullName', 'id')->toArray();

On View:

{{ Form::select('Instructors', $instructorOptions, null, ['class'=>'form-control']) }}
1 like

Please or to participate in this conversation.