Define the selected option with the old input in Laravel / Blade

后端 未结 14 904
孤街浪徒
孤街浪徒 2020-12-04 22:04

I have this code:


                        
    
提交评论

  • 2020-12-04 22:17
    <select style="width: 100%;" name="id_driver" id="id_driver" >
      <option value="" @if (old('id_driver') == "")  selected @endif>Select</option>
      @foreach(\App\Driver::all() as $driver)
        <option value="{{$driver->id}}" @if (old('id_driver') == $driver->id)  
            selected  @endif >(#{{$driver->id}}) {{$driver->business_name}}
        </option>
      @endforeach
    </select>
    
    0 讨论(0)
  • 2020-12-04 22:18

    Laravel 6 or above: just use the old() function for instance @if (old('cat')==$cat->id), it will do the rest of the work for you.

    How its works: select tag store the selected option value into its name attribute in bellow case if you select any option it will store into cat. At the first time when page loaded there is nothing inside cat, when user chose a option the value of that selected option is stored into cat so when user were redirected old() function pull the previous value from cat.

     {!!Form::open(['action'=>'CategoryController@storecat', 'method'=>'POST']) !!}
            <div class="form-group">
                <select name="cat" id="cat" class="form-control input-lg">
                    <option value="">Select App</option>
                    @foreach ($cats as $cat)
                        @if (old('cat')==$cat->id)
                            <option value={{$cat->id}} selected>{{ $cat->title }}</option>
                        @else
                            <option value={{$cat->id}} >{{ $cat->title }}</option>
                        @endif
                    @endforeach
                </select>
            </div>
    
            <div class="from-group">
                {{Form::label('name','Category name:')}}
                {{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
            </div>
        <br>
        {!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
        {!!Form::close()!!}
    
    0 讨论(0)
  • 2020-12-04 22:18
          <select class="form-control" name="kategori_id">
            <option value="">-- PILIH --</option>
            @foreach($kategori as $id => $nama)
                @if(old('kategori_id', $produk->kategori_id) == $id )
                <option value="{{ $id }}" selected>{{ $nama }}</option>
                @else
                <option value="{{ $id }}">{{ $nama }}</option>
                @endif
            @endforeach
            </select>
    
    0 讨论(0)
  • 2020-12-04 22:19

    this will help you , just compare with old if exist , if not then compare with the default value

    <select name="select_name">
        @foreach($options as $key => $text)
           <option {{ ($key == old('select_name',$default))?'selected':'' }}> {{ $text }} </option>
        @endforeach
    </select>
    

    the $default is the value that injected from the controller to the view

    0 讨论(0)
  • 2020-12-04 22:20

    The solution is to compare Input::old() with the $keyvariable.

    @if (Input::old('title') == $key)
          <option value="{{ $key }}" selected>{{ $val }}</option>
    @else
          <option value="{{ $key }}">{{ $val }}</option>
    @endif
    
    0 讨论(0)
  • 提交回复
    热议问题