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

danny620's avatar

Populate Dropdown

Hi,

I'm trying to build a custom dropdown for a laravel form in my controller It's working fine but it is giving me 0 and a 1 in the dropdown, please see.

public function advisorBook(Location $location){

    $userList = [];
    $users = Advisor::where('advisor_id', '=', Auth::user()->id)->where('accepted', '=', 1)->get();

    foreach ($users as $user){
        $userList[] = array($user->user->id => $user->user->full_name . ' | ' . $user->user->email);
    }
    
    return view('backend.advisors.booking.form', compact('userList'));
}

this is the HTML its outputting.

<select name="user">
<optgroup label="0">
    <option value="140">Daniel Mills | daniel620@hotmail.co.uk</option>
</optgroup>
<optgroup label="1">
    <option value="150">Paul Mills | [email protected]</option>
</optgroup>
</select>
0 likes
4 replies
jekinney's avatar

That's not how opt groups work. Don't use a value on them.

http://www.w3schools.com/Tags/tag_optgroup.asp

Array loop in oop???? Collections and objects should be your best friend. Your looping through a collection to out put an array, why not just use a more efficient query?

danny620's avatar

I just want to show the dropdown list without the headings

Please or to participate in this conversation.