Laravel 表单 - 填写多个 Select 输入以查看

Laravel Forms - Fill a Multiple Select Input to View

我这里有一个棘手的情况。

我有一个编辑表单,可以在编辑索引时填写现有值。 (我在控制器中传递了一个模型对象)。它适用于每个输入字段,但我需要填写某个我不知道该怎么做的 Multiple Select 字段。我尝试在对象中传递这个特定值,或者作为数组(包含每个 selectable 值的 ID)使视图崩溃,或者作为使用 json_encode 的 json 字符串。我在这里读了一篇文章,其中有人完成了这项工作,但它是通过多对多关系 table 完成的,但我需要填写的值并非来自 table。

使用 dd() 生成的对象

Contact {#408 ▼
  +table: "contacts"
  #dates: array:1 [▼
    0 => "deleted_at"
  ]
  +fillable: array:5 [▼
    0 => "nombre"
    1 => "apellido"
    2 => "cargo"
    3 => "empresa_id"
    4 => "groups"
  ]
  #casts: array:5 [▼
    "nombre" => "string"
    "apellido" => "string"
    "cargo" => "string"
    "empresa_id" => "integer"
    "groups" => "array"
  ]
  #connection: null
  #primaryKey: "id"
  #keyType: "int"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:9 [▼
    "id" => 4
    "nombre" => "redacted"
    "apellido" => "redacted"
    "cargo" => "redacted"
    "empresa_id" => 2
    "created_at" => "2017-03-09 20:30:19"
    "updated_at" => "2017-11-29 17:30:55"
    "deleted_at" => null
    "groups" => "["2","3","4"]"
  ]
  #original: array:9 [▼
    "id" => 4
    "nombre" => "redacted"
    "apellido" => "redacted"
    "cargo" => "redacted"
    "empresa_id" => 2
    "created_at" => "2017-03-09 20:30:19"
    "updated_at" => "2017-11-29 17:30:55"
    "deleted_at" => null
    "groups" => "["2","3","4"]"
  ]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #dateFormat: null
  #touches: []
  #observables: []
  #with: []
  +exists: true
  +wasRecentlyCreated: false
  #forceDeleting: false

多个Select表格。

{!! Form::label('groups', 'Grupos:') !!} </br>
{!! Form::select('groups', $groups, null, ['data-actions-box' => 'true', 'multiple'=>'multiple']) !!}

如果需要更多信息,我会编辑问题。

通过在将数组元素传递给 json_encode.

之前将数组元素转换为 int 来修复它