Laravel从数据库中选择optgroup和分隔值

时间:2016-07-22 23:32:57

标签: laravel select optgroup laravelcollective

我写表单将值发送到数据库:

{!! Form::model($product, ['method'=>'PATCH','class'=>'form-horizontal','action'=>['ProductsController@updatechangeStatus', $product->id]]) !!}
        {{ Form::select('userstan_id', array(
            'Użytkownicy' => array($userList),
            'Magazyny' => array($warehouseList),
        ), null, ['class' => 'form-control'])}}
        {!! Form::submit('Tak, przenieś', ['class' => 'btn btn-primary']) !!}
        {!! link_to('warehouse/'. $product->id, $title = 'Anuluj', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

public function changestatus($id){
    $product = Product::find($id);
    $userList = User::lists('name', 'id');
    $warehouseList = warehouse::lists('name_warehouse', 'id');
    return view('pages.changestatus', compact('userList', 'product', 'warehouseList'));
}

Laravel在Form::select中返回了这样的值:http://iv.pl/images/97320689105137896288.png

但我想返回这样的结果: http://iv.pl/images/48707724105931389272.png

我怎样才能返回名称用户和名称仓库,而值是Laravel中的id号码(我使用foreach,但是数组foreach不工作),如下所示:

<select>
  <optgroup label="Użytkownicy">
    <option value="1">michal</option>
    <option value="2">mateusz</option>
  </optgroup>
  <optgroup label="Magazyny">
    <option value="1">kosz</option>
    <option value="2">zaginione</option>
  </optgroup>
</select>

1 个答案:

答案 0 :(得分:0)

好的我写这个:(现在Laravel返回记录正确的外观:klik!

<select name="userstan_id" class="form-control">
  <optgroup label="Użytkownicy">
    @foreach($userListName as $user)
        <option value="{{ $user->id }}">{{ $user->name }}</option>
    @endforeach
  </optgroup>

  <optgroup label="Magazyny">
    @foreach($warehouseList as $warehouse)
        <option value="{{ $warehouse->id }}">{{ $warehouse->name_warehouse }}</option>
    @endforeach
  </optgroup>
</select>

其中name="userstan_id"是要保存或更新记录的列的名称;)