Laravel Forms - 填充多选输入以查看

时间:2017-11-30 13:22:29

标签: laravel blade

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

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

使用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

多选表格。

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

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

1 个答案:

答案 0 :(得分:0)

通过将数组元素转换为 int ,然后将其传递给json_encode来修复它。