使用laravel 5.2在php上设置select2的值

时间:2017-05-05 11:02:07

标签: laravel jquery-select2

在我的控制器中

$this->selectedUsers = UserTipepermintaanRelationship::where('permintaan_tipe_id', $id)->pluck('user_id', 'user_id');

上面的对象将给出输出:

1 => 1
2 => 2
25 => 25

在我的html中:

{!! Form::select('user_id[]', $users, isset($tipePermintaan) ? $selectedUsers : null, ['class' => 'form-control select-user', 'multiple']) !!}

我想用selectedUsers设置我的select2多重值。

但我无法在select2倍数上设置值。我哪里做错了?

2 个答案:

答案 0 :(得分:0)

我认为您需要更新代码,如:

{!! Form::select('user_id[]', $users, (isset($tipePermintaan) ? $selectedUsers : null), ['class' => 'form-control select-user', 'multiple']) !!}

答案 1 :(得分:0)

$this->selectedUsers是集合,但对于Form::select,第三个参数必须是带键的数组。所以你需要:

$this->selectedUsers = UserTipepermintaanRelationship::where('permintaan_tipe_id', $id)->pluck('user_id')->toArray();