如何将字段中多重选择的值存储为laravel中的字符串

时间:2018-01-31 10:09:58

标签: javascript php laravel-5

{!! Form::open(['route'=>'diagnostic.store']) !!} 
this is my sight:

<select class="form-control" multiple="multiple" name="diagnóstico_rela" id="person3">
  <option selected="selected">orange</option>
  <option>white</option>
  <option selected="selected">purple</option>
</select>
<script>


$("#person3").select2({

    tags: true,


})


</script>

{!! Form::close() !!}

这是我的控制者:

$diagnosticRear_segment = new Rear_segment;
$diagnosticRear_segment->conducta = $request->conducta;
$diagnosticRear_segment->principal_diagnostic_id = $request->principal_diagnostic_id;
$diagnosticRear_segment->diagnóstico_rela = $request->diagnóstico_rela;
$diagnosticRear_segment->ultimo_ontrol = $request->ultimo_ontrol;
$diagnosticRear_segment->próximo_control = $request->próximo_control;
$diagnosticRear_segment->save();

我是新手,我感谢你提供的帮助 在控制器中,我显示了我在该表中保存的所有字段,在视图中我显示的字段导致了我的问题

1 个答案:

答案 0 :(得分:0)

在您的刀片文件中,将数组符号[]添加到name标记的属性select

<select class="form-control" multiple="multiple" name="diagnóstico_rela[]" id="person3">
  <option selected="selected">orange</option>
  <option>white</option>
  <option selected="selected">purple</option>
</select>

在您的控制器中,$request->diagnóstico_rela将返回所有选定项目的数组。 然后,您可以使用implode将其转换为如下字符串:

$diagnosticRear_segment->diagnóstico_rela = implode(', ', $request->diagnóstico_rela);