如何将多个复选框填充到数据库?
How to populate multiple Checkboxes from to Database?
我有一个语音模型,我在其中保存所有新语音,每个语音都有名称和语言字段。我的第二个模型是设备模型,我想在设备表单视图中将这些声音填充为复选框,让用户选择一个或多个并将其保存到相关设备中,如 JSON。
有人 - 帮助! :)
我要怎么把声音保存到机器人
{
"voice_name":"sample1",
"voice_language":"English"
}
语音模型
public $fillable = [
'name',
'language'
];
public function devices()
{
return $this->belongsToMany('App\Models\Device');
}
设备型号
public $fillable = [
'device_name',
'version',
'voices'
];
public function voices()
{
return $this->hasMany('App\Models\Voice');
}
机器人形态
<div class="form-group">
{!! Form::label('device_name', 'Device Name:') !!}
{!! Form::text('device_name', null, ['class' => 'form-control') !!}
{!! Form::label('version', 'version:') !!}
{!! Form::text('version', null, ['class' => 'form-control') !!}
<div class="form-group">
{!! Form::label('voices', 'Voices:') !!}
//I want to list all the voices here as Checkboxes
{!! Form::checkbox('voices[]', null, ['class' => 'form-control']) !!}
</div>
我用这个selectinput.blade.php
<input type="hidden" name="{{$attr['id']}}" value="0" />
<input type="checkbox" class="filled-in"
@foreach ($attr as $attr_key => $attr_value)
@if ($attr_key == 'disabled')
@if($attr_value == 'disabled')
disabled="disabled"
@endif
@else
@if ($attr_key == 'readonly')
@if($attr_value == 'readonly')
readonly="readonly" disabled="disabled"
@endif
@else
{{$attr_key}}="{{$attr_value}}"
@endif
@endif
@endforeach
@if ($val=="1")
checked
@endif
@if (isset($attr['id']))
name="{{ $attr['id'] }}"
@endif value="1">
@if (isset($attr['id']))
<label for="{{ $attr['id'] }}" >
{{ $label }}
</label>
@endif
其中有 2 个复选框作为一种技巧,即使未选中也始终发送复选框值,based on this answer。
我有一个语音模型,我在其中保存所有新语音,每个语音都有名称和语言字段。我的第二个模型是设备模型,我想在设备表单视图中将这些声音填充为复选框,让用户选择一个或多个并将其保存到相关设备中,如 JSON。
有人 - 帮助! :)
我要怎么把声音保存到机器人
{
"voice_name":"sample1",
"voice_language":"English"
}
语音模型
public $fillable = [
'name',
'language'
];
public function devices()
{
return $this->belongsToMany('App\Models\Device');
}
设备型号
public $fillable = [
'device_name',
'version',
'voices'
];
public function voices()
{
return $this->hasMany('App\Models\Voice');
}
机器人形态
<div class="form-group">
{!! Form::label('device_name', 'Device Name:') !!}
{!! Form::text('device_name', null, ['class' => 'form-control') !!}
{!! Form::label('version', 'version:') !!}
{!! Form::text('version', null, ['class' => 'form-control') !!}
<div class="form-group">
{!! Form::label('voices', 'Voices:') !!}
//I want to list all the voices here as Checkboxes
{!! Form::checkbox('voices[]', null, ['class' => 'form-control']) !!}
</div>
我用这个selectinput.blade.php
<input type="hidden" name="{{$attr['id']}}" value="0" />
<input type="checkbox" class="filled-in"
@foreach ($attr as $attr_key => $attr_value)
@if ($attr_key == 'disabled')
@if($attr_value == 'disabled')
disabled="disabled"
@endif
@else
@if ($attr_key == 'readonly')
@if($attr_value == 'readonly')
readonly="readonly" disabled="disabled"
@endif
@else
{{$attr_key}}="{{$attr_value}}"
@endif
@endif
@endforeach
@if ($val=="1")
checked
@endif
@if (isset($attr['id']))
name="{{ $attr['id'] }}"
@endif value="1">
@if (isset($attr['id']))
<label for="{{ $attr['id'] }}" >
{{ $label }}
</label>
@endif
其中有 2 个复选框作为一种技巧,即使未选中也始终发送复选框值,based on this answer。