Setting selected option in laravel form

后端 未结 14 985
春和景丽
春和景丽 2020-12-25 14:09

I need to give selected value like this html:


                        
    
提交评论

  • 2020-12-25 14:16

    You can do it like this.

    <select class="form-control" name="resoureceName">
    
      <option>Select Item</option>
    
      @foreach ($items as $item)
        <option value="{{ $item->id }}" {{ ( $item->id == $existingRecordId) ? 'selected' : '' }}> {{ $item->name }} </option>
      @endforeach    </select>
    
    0 讨论(0)
  • 2020-12-25 14:18

    use this package and check the docs:

    https://laravelcollective.com/docs/5.2/html#drop-down-lists

    form you html , you need use this mark

    {!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); !!}
    
    0 讨论(0)
  • 2020-12-25 14:19

    If anyone is still here, here is my simple version.

    <select name="status" class="js-example-basic-single w-100">
                  <option value="" @if($brand->status == '') ? selected : null @endif disabled>Choose what to be seen on brand section</option>
                  <option value="TA" @if($brand->status == 'TA') ? selected : null @endif>Title Active</option>
                  <option value="ITA" @if($brand->status == 'ITA') ? selected : null @endif>Image Title Active</option>
                 
                </select>
    
    0 讨论(0)
  • 2020-12-25 14:20
                @foreach ($categories as $category)
                 <option value="{{$category->id}}" 
                   @foreach ($posts->postRelateToCategory as $Postcategory)
                     @if ($Postcategory->id == $category->id)
                     {{'selected="selected"'}}
                     @endif 
                   @endforeach >
                  {{ $category->category_name }} </option>               
                @endforeach    
    
    0 讨论(0)
  • 2020-12-25 14:21

    Try this

    <select class="form-control" name="country_code" value="{{ old('country_code') }}">
       @foreach (\App\SystemCountry::orderBy('country')->get() as $country)
           <option value="{{ $country->country_code }}"
               @if ($country->country_code == "LKA")
                 {{'selected="selected"'}}
               @endif
           >
              {{ $country->country }}
           </option>
       @endforeach
    </select>
    
    0 讨论(0)
  • 提交回复
    热议问题