Laravel编辑表单 - 通过数据库获取下拉列表的值

时间:2018-02-25 19:58:40

标签: laravel laravel-5

我有一个用户提交表单,其中一些字段是下拉列表

                <div class="form-group row">
                  <label for="type" class="col-md-3 col-form-label text-md-right">Type</label>
                    <div class="col-md-9">
                      <select class ="form-control" id="type" name="type">
                        <option>Apartment</option>
                        <option>House</option>
                        <option>Studio</option>
                        <option>Flat</option>
                      </select>
                      @if ($errors->has('type'))
                          <span class="invalid-feedback">
                              <strong>{{ $errors->first('type') }}</strong>
                          </span>
                      @endif
                    </div>
                </div>

这很好用。

我没有尝试允许用户编辑特定表单。我可以通过为输入分配值并像这样调用数据

来获取标题和照片等其他部分
<input id="Address" type="text" class="form-control" name="address" value="{{$Advert->address }}" required autofocus>

但是当我尝试在选择选项上做类似的事情时,什么都没有出现。这是编辑页面上的下拉列表。

<div class="form-group row">
                          <label for="type" class="col-md-3 col-form-label text-md-right">Type</label>
                            <div class="col-md-9">
                              <select class ="form-control" id="type" name="type" value="{{$Advert->type}}">
                                <option></option>
                                <option>Apartment</option>
                                <option>House</option>
                                <option>Studio</option>
                                <option>Flat</option>
                              </select>
                              @if ($errors->has('type'))
                                  <span class="invalid-feedback">
                                      <strong>{{ $errors->first('type') }}</strong>
                                  </span>
                              @endif
                            </div>
                        </div>

现在我可以用硬编码的价值来做我需要的事情

<select class ="form-control" id="type" name="type">
      <option value="">Select</option>
      <option {{ $Advert->type == 'Apartment' ? 'selected':'' }}>Apartment</option>
      <option {{ $Advert->type == 'House' ? 'selected':'' }}>House</option>
      <option {{ $Advert->type == 'Studio' ? 'selected':'' }}>Studio</option>
      <option {{ $Advert->type == 'Flat' ? 'selected':'' }}>Flat</option>
 </select>

但是我没有使用查询数据库的每个循环

  @foreach ($Advert as $type)
                                      <option {{ $type->type == '{{$type->type}}' ? 'selected':'' }}>{{$type->type}}</option>
                                    @endforeach

2 个答案:

答案 0 :(得分:0)

您的代码应如下所示:

@foreach ($types as $type)
  <option {{ $type->type == $Advert->type ? 'selected':'' }}>{{$type->type}}</option>
@endforeach

答案 1 :(得分:0)

尝试使用以下代码:

@foreach ($types as $type)
  <option @if($type->type == $Advert->type) selected @endif value="{{$type->type}}">{{$type->type}}</option>
@endforeach