如何在Laravel中创建下拉菜单

时间:2019-03-03 06:55:43

标签: laravel

我要在Laravel中下拉一列,创建该列时应在下拉菜单中显示所有国家,出于更新目的,应选择当前国家。 任何示例或教程都会有所帮助,谢谢。 我的尝试:

<select name="nation" class="custom-select" >
      <option selected value="">Choose...</option>
      <option value="{{@$teacher->nation}}"  {{@$teacher->nation== "Pakistan" ? 'selected' : ''}} >{{@$teacher->nation}}</option>
      </select>

问题:我的下拉列表为空。

3 个答案:

答案 0 :(得分:1)

<select name="nation" class="custom-select">
    <option selected value="">Choose...</option>
    @foreach($teachers as $teacher)
        <option value="{{ $teacher->nation }}"  {{ $teacher->nation == "Pakistan" ? 'selected' : '' }} >{{ $teacher->nation }}</option>
    @endforeach
</select>

答案 1 :(得分:0)

这是laravel中的下拉结构

 <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>

答案 2 :(得分:0)

 <select name="nation" class="custom-select" >
     <option value="">Choose...</option>
        @foreach ($teachers as $key => $value)
            <option value="{{ $key }}" {{ old('nation') == $key ? 'selected' : ''}}>{{ $value }}</option>
        @endforeach
 </select>