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

Alizey's avatar

Select DropDown Option Seleted In Laravel

How can i show selected dropdown Option which user has selected .

 <select class="form-control m-bot15" name="role_id">
           @if($roles->count() > 0)
          @foreach($roles as $role)
           <option value="{{$role->id}}">{{$role->name}}</option>
          @endForeach
          @else
           No Record Found
            @endif   
        </select>

How can i show the selected value that comes from db ?

0 likes
12 replies
bobbybouwmann's avatar
Level 88

You can do something like this

// Controller

public function index()
{
    $roles = Roles::all();
    $selectedRole = User::first()->role_id;

    return view('my_view', compact('roles', 'selectedRole');
}

And then in your view

<select class="form-control m-bot15" name="role_id">

    @if ($roles->count())

        @foreach($roles as $role)
            <option value="{{ $role->id }}" {{ $selectedRole == $role->id ? 'selected="selected"' : '' }}>{{ $role->name }}</option>    

    @endif

</select>
17 likes
Alizey's avatar

@blackbird its giving syntax error

syntax error, unexpected '='

 <option value={{ $role->id }}" {{ $selectedRole == $role->id ? selected="selected" : '' }}>{{ $role->name }}</option> 

There

RachidLaasri's avatar

This is a better way to do it

$roles = Roles::lists('name', 'id');

HTML

{!! Form::select('role_id', $roles, $selectedRole, ['class' => 'form-control m-bot15') !!}
2 likes
RachidLaasri's avatar

... And if you want to fix your code

<option value="{{ $role->id }}" {{ $selectedRole == $role->id ? selected="selected" : '' }}>{{ $role->name }}</option> 
bobbybouwmann's avatar

I fixed my answer!

@RachidLaasri I wanted to show that way as well, but I was just on my way to go home from work :P And since he wasn't using any kind of the Form facade I didn't show that way instead ;)

Snapey's avatar

@jhansi .. don't post to a year-old topic. Start a new one with your specific issue

jhansi's avatar

@snapey I started new discussion like this"Please tell me How to Select DropDown Option Selected In Laravel" but I have found no solution,So i came back for solution.

Hash's avatar

marked answer still helped me with Laravel 5.6 :D Thanks alot!

Behinder's avatar

@RACHIDLAASRI - This doesnt work as of 5.8 because it shows: syntax error unexpected = Looks this is non-trivial.

Snapey's avatar

@behinder you would be better posting your own question.

This one is FOUR years old. In the interim, ->lists() has been replaced by ->pluck()

Go3shom's avatar

As per Laravel 9 docs, you may use @selected directive like so:

<select class="form-control m-bot15" name="role_id">
    @if ($roles->count() > 0)
        @foreach ($roles as $role)
            <option value="{{ $role->id }}" @selected(old('role_id') == $role)>
                {{ $role->name }}
            </option>
        @endForeach
    @else
        No Record Found
    @endif
</select>

Please or to participate in this conversation.