Search code examples
phplaravellaravel-5.3

How to get by default select value in dropdown in Edit mode laravel


In edit mode my selecte dropdown like this.

<select  name="comic_publisher" id="publishers" >
            @foreach($group as $team)
                <option value="{{$team['group_id']}}">{{$team['comic_group_name']}}</option>
            @endforeach
      </select>

and I want like if I edit the record the selected value associated with that record is by default here should be selected.

I am stuck with the little issue, can anyone please help me how to do that.

Thanks a lot.


Solution

  • You need to retrieve first database inserted value, and then check that value with all options if match found you can select like,

    $db_selected_value = "2"; // retrieved data
    <select name="comic_publisher" id="publishers" >
            @foreach($group as $team)
                <option value="{{$team['group_id']}}" @if($db_selected_value == $team['group_id']) {{ 'selected' }} @endif>{{$team['comic_group_name']}}</option>
            @endforeach
      </select>
    

    here, @if($db_selected_value == $team['group_id']) {{ 'selected' }} @endif this line matches your old selected value to all the other. and selected selects