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

noblemfd's avatar

How to set default value in select dropdown option

I have this code

Controller:

public $leave_applicable_genders = [
    "1" => "Both",
    "2" => "Male",
    "3" => "Female",
]; 

public function create()
{
    $leavetype = new HrLeaveType();
    
    return view('leave.leave_types.create')
            ->with('leavetype', $leavetype)
            ->with('leave_applicable_genders', $this->leave_applicable_genders);
}

view:

     <td width="15%">                    
          <select class="form-control select2bs4" data-placeholder="Select Applicable Gender" tabindex="1" name="leave_applicable_gender[]">
               @foreach($leave_applicable_genders as $k => $leave_applicable_gender)
                   <option value="{{$k}}" @if(old("leave_applicable_gender") == "$k") selected @endif>{{$leave_applicable_gender}}</option>
               @endforeach                                         
           </select>
     </td>     
  1. How do I set the default value of the select dropdown option to 1 (Both)?

  2. The old() helper is not working. How do I rectify this?

Thanks

0 likes
2 replies
itsfg's avatar

Hi @noblemfd

Is your select a multiple select ? It doesn't seems so, but then you should change the input name to leave_applicable_gender (without [])

To have a defaut value for your first page access, you should set the second parameter of old(). This second parameter is the value returned by old if the request doesn't contain this input.

 @if(old('leave_applicable_gender', 1) == $k) //no need to put "" around $k

Please or to participate in this conversation.