如何将多个Checkbox填充到数据库?

时间:2017-02-24 20:54:48

标签: php json laravel eloquent blade

我有一个语音模型,我保存所有新的声音,每个声音都有名称和语言字段。我的第二个模型是设备模型,我想在设备表单视图中将这些声音填充为复选框,并让用户选择一个或多个,并将其作为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>

1 个答案:

答案 0 :(得分:0)

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

如果有两个复选框作为一种黑客总是发送复选框值,即使它未经检查,based on this answer