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

SYPOMark's avatar

Populating select options from database

Hi there,

As the title suggests, I am trying to create a select box in a form, the options of which are populated from a database table.

After some looking around, I found [this post on Stack Overflow] (https://stackoverflow.com/questions/35421804/laravel-5-2-populate-select-options-from-database) and have edited my code as follows.

The view, which is found at:

/laravel/resources/views/vendor/spark/settings/mystuff/list-stuff.blade.php

contains this:

<select name="stuff_type">
    @foreach($stuff_types as $stuff_type)
        <option value="{{ $stuff_type->id }}">{{ $stuff_type->name}}</option>
    @endforeach 
</select>

The Controller contains this, amongst other things:

...
use Illuminate\Support\Facades\DB;
...

    public function index()
    {
        $stuff_types = DB::table('stuff_type')->get();

        return view('settings.mystuff.list-stuff', ['stuff_type' => $stuff_type]);
    }

So, as far as I can see, I've done all that was asked in the answer on the Stack Overflow post. But, on visiting the page that uses the view, I get an error:

Undefined variable: stuff_types

I am BOUND to have missed a step somewhere, but don't know what it is. Do I need to edit routes.php or the appropriate js file?

I am using Laravel Spark version 3.0 with Laravel version 5.3

Any help that anyone can provide would be most appreciated.

With kind regards,

Mark

0 likes
6 replies
abusalameh's avatar

just rename the returned variable

...
use Illuminate\Support\Facades\DB;
...

    public function index()
    {
        $stuff_types = DB::table('stuff_type')->get();

        return view('settings.mystuff.list-stuff', ['stuff_type' => $stuff_type]);
    }

to 'stuff_types' => '$stuff_types'

3 likes
sujancse's avatar

Change ['stuff_type' => $stuff_type] to ['stuff_type' => $stuff_types] You named your object $stuff_types which is plural and get it wrong as singular $stuff_type in return view.

SYPOMark's avatar

Hi, @abusalameh and @st8113

Thank-you both for coming back to me so soon.

I have tried both of your suggestions and unfortunately I am still getting the same error.

Is there anything else I might have done incorrectly or missed out?

With kind regards,

Mark

Cronix's avatar
public function index()
{
    $stuff_types = DB::table('stuff_type')->get();

    return view('settings.mystuff.list-stuff', ['stuff_types' => $stuff_types]);
}

$stuff_types should now be available to the view.

The array that you are passing to the view is made up of key/value pairs, where the key 'name' will become the variable $name in the view. ['key' => $value]

so if you had the following: $value = 'hello';

and sent this array to your view: ['key' => $value], when you echo $key in the view it will output 'hello'. Hope that clears it up a bit.

SYPOMark's avatar

Hi @Cronix

Thank-you too for your help. The function in the controller reads just as you say, but, unfortunately, I still get the error.

Should the route to the blade be longer? So:

return view('vendor.spark.settings.mystuff.list-stuff'

as it is found at:

/laravel/resources/views/vendor/spark/settings/mystuff/list-stuff.blade.php

I will have to look at this again, anew, tomorrow.

It's home time for me now.

With kind regards,

Mark

SYPOMark's avatar
SYPOMark
OP
Best Answer
Level 1

Thanks all for your help.

I eventually managed to get this working, but it required me to edit & create a lot more files than any of the other answers suggest. This is probably due to the fact that I'm not just building with Laravel, but with Laravel Spark which makes extensive use of Vue JS.

If anyone else is struggling with this, post here, and I'll get back to you as soon as I can with the full answer. As a starter, here's what I had to put in the Blade:

<select name='stuff_type_id' v-model='form.stuff_type_id' class='form-control'>
    <option value=''>Please choose one...</option>
    <option v-for='stufftype in stufftypes' v-bind:value='stufftype.id'>@{{ stufftype.name }}</option>
</select>

After that, as I say, I'm afraid it's a LOT more than just a simple edit of the Controller.

I hope this helps someone and points them in the right direction.

With kind regards,

Mark

Please or to participate in this conversation.