在Laravel中从数据库创建一个Select Box表单

时间:2015-01-08 20:09:29

标签: laravel laravel-4

我在这个表单中有2个选择框,第一个选择框已完成且有效。

...但是当我添加第二个选择框时,错误就像图片一样..

这是代码

这是第一个选择框 - 它有效:

{{ Form::open(array('url' =>route('units.store'), 'method' => 'post' ))}}

  <div class="form-group @if ($errors->has('brand_id')) has-error @endif">
    <label>Brand Manufacture</label>
    {{Form::select('brand_id', $brand, null, ['class' => 'form-control w450']) }}
    @if ($errors->has('brand_id')) <p class="help-block">{{ $errors->first('brand_id') }}</p> @endif
  </div>

这是不正确的选择:

 <div class="form-group @if ($errors->has('model_id')) has-error @endif">
    <label>Model Type</label>
    {{Form::select('model_id',$model, null, ['class' => 'form-control w450']) }}
    @if ($errors->has('model_id')) <p class="help-block">{{ $errors->first('model_id') }}</p> @endif
  </div>

这就是整个代码。

  {{ Form::open(array('url' =>route('units.store'), 'method' => 'post' ))}}

  <div class="form-group @if ($errors->has('brand_id')) has-error @endif">
    <label>Brand Manufacture</label>
    {{Form::select('brand_id', $brand, null, ['class' => 'form-control w450']) }}
    @if ($errors->has('brand_id')) <p class="help-block">{{ $errors->first('brand_id') }}</p> @endif
  </div>


  <div class="form-group @if ($errors->has('model_id')) has-error @endif">
    <label>Model Type</label>
    {{Form::select('model_id',$model, null, ['class' => 'form-control w450']) }}
    @if ($errors->has('model_id')) <p class="help-block">{{ $errors->first('model_id') }}</p> @endif
  </div>

  <div class="form-group @if ($errors->has('kva')) has-error @endif">
    <label>kva</label>
    <input name="kva" type="text" class="form-control w450"  value="{{ Input::old('kva') }}">
    @if ($errors->has('kva')) <p class="help-block">{{ $errors->first('kva') }}</p> @endif
  </div>

  <div class="form-group @if ($errors->has('type')) has-error @endif">
    <label>Type</label>
    <select name="type" id="" class="form-control w450">
      <option value="OPEN">OPEN</option>
      <option value="CLOSE">CLOSE</option>
    </select>
    @if ($errors->has('type')) <p class="help-block">{{ $errors->first('type') }}</p> @endif
  </div>

  <div class="form-group @if ($errors->has('sn')) has-error @endif">
    <label>Serial Number</label>
    <input name="sn" type="text" class="form-control w450"  value="{{ Input::old('sn') }}">
    @if ($errors->has('sn')) <p class="help-block">{{ $errors->first('sn') }}</p> @endif
  </div>

   <div class="form-group @if ($errors->has('wbs_unit')) has-error @endif">
    <label>WBS unit</label>
    <input name="wbs_unit" type="text" class="form-control w450"  value="{{ Input::old('wbs_unit') }}">
    @if ($errors->has('wbs_unit')) <p class="help-block">{{ $errors->first('wbs_unit') }}</p> @endif
  </div>

  <div class="form-group">
      <button type="submit" class="btn btn-sm btn-primary">create</button>
  </div>

{{ Form::close() }}

我无法发布任何图片:(

所以请帮帮我..请

1 个答案:

答案 0 :(得分:0)

确保以正确的格式从数据库中提取数据以供选择框使用。它应该是一个关联数组,其中键是数据库ID,值是要在下拉列表中显示的字符串。这是一个例子。

$brands = Brand::lists('name', 'id');
// Example: ['1' => 'Adidas', '2' => 'Nike', '3' => 'New Balance;];

{{ Form::select('brand_id', $brands) }}

此外,您可以使用三元语句简化错误类,如下所示。

<div class="form-group {{ $errors->has('brand_id') ? 'has-error' : null }}">