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

Ober4You's avatar

Form select box {!!Form::select()!!}

I'm using Laravel Collective Form builder in my view and i'm populating this select box from a table in my DB like this

{!!Form::select('worktype_id[]', $worktypes_list, null, ['class' => 'form-control'])!!}

In my controller i have this for populating that select box

$worktypes_list = Worktype::lists('id', 'name');
return view('edit')->compact(''worktypes_list');

Now when i want to edit the selected item is always the first one and it should be the one that is saved in DB.

How to make selected item to be the one which is saved in DB?

0 likes
11 replies
tykus's avatar
tykus
Best Answer
Level 104

Change the null (3rd) argument to the saved worktypes; e.g.

{!!Form::select('worktype_id[]', $worktypes_list, $thing->worktypes->pluck('id'), ['class' => 'form-control'])!!}
2 likes
Ober4You's avatar

Thank you very much! It works flawlessly :)

Now, one question out of the topic....where can i read or if you could tell me how to format code like you did? So that it has black background and keywords in other color? Here on forum.

Ober4You's avatar

Hmmm but it's not black :)

I just can't get it right

tykus's avatar

It is for me :D

You need three backticks on their own line, then your code, and finally close out the backticks

1 like
Ober4You's avatar

I have another problem now....when i pull data from my DB like this

{!! Form::select('name', $worktypes->pluck('name'), $workedtime->worktype->id, ['class' => 'form-control']) !!}

the select options have values from 0 to 12 and i want to have them from 1 to 13

How can i set values start from 1 instead from 0?

<option value="0" selected="selected">first</option>
<option value="1">second</option>
<option value="2">third</option>
<option value="3">fourth</option>
<option value="4">fifth</option>
<option value="5">sixth</option>
<option value="6">seventh</option>
<option value="7">eight</option>
...
Bom_Basti's avatar

Could you explain why you want to do this?

If you change the values of the option fields you have to reduce this value by one in the controller to communicate with the database.

Ober4You's avatar

what i really want is to have value from select correspond to ID of selected value from DB

it should be:

<option value="3">third</option>

and not

<option value="2">third</option>

So in default Form builder:

{!! Form::select('name', $worktypes->pluck('name'), $workedtime->worktype->id, ['class' => 'form-control']) !!}

first comes the name then the values of select and then default selected i just want to somehow get rid of 0...or set it to something else....or maybe array_merge....but i don't know how to do it....maybe someone did this before

m3enkech's avatar

if you want to keep the same id for each element use Pluck : Controller : $worktypes= Worktype::pluck('name','id'); return view('edit')->compact(''worktypes');

View: {!! Form::select('name', $worktypes, null, ['class' => 'form-control']) !!} this will display each name with a ID on value

udochify's avatar

too bad that blade - as powerful as it is - is just a bag of code with no documented specifications. Those found on the laravel.com site are sparse - nothing said about selects. even the official pdf doc has a long section on the blade templating engine but never talked about forms at all. so to know where what and what fits in to where, you have to start scribbling through the illuminate folder and go through an unending list of class declarations. it's a real hassle. I'd rather stick to html 5.

forteirp's avatar

Like #Ober4You,

I having the same issues with my values not matching up and my dropdown is also giving me the values as an array...

Here's what I have in my PostsController...

public function edit(Post $post)
{
    $categories = Category::all()->pluck('title', 'id')->toArray();

    return view('posts.edit')->withPost($post)->withCategories($categories);

and here's my edit.blade...

{{ Form::label('category_id', 'Category :')}}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}

I have tried the few different ways suggested above, but I'm still having these issues, "need little help"?

Please or to participate in this conversation.