donglin4636 2017-02-24 20:54
浏览 58
已采纳

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

I have a Voice Model where I save all the new voices and each voice has name and language field. My Second Model is Device Model, I want to populate these voices on the Device form view as Checkboxes and let the user to pick one or more than one and save it to related Device as JSON.

Someone - help! :)

How I want to save the voices to the Robot

{
  "voice_name":"sample1",
  "voice_language":"English"
}

Voice Model

public $fillable = [
  'name',
  'language'
];

public function devices()
{
  return $this->belongsToMany('App\Models\Device');
}

Device Model

public $fillable = [
  'device_name',
  'version',
  'voices'
];

public function voices()
{
  return $this->hasMany('App\Models\Voice');
}

Robot Form

<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条回答 默认 最新

  • dongziche8030 2017-02-24 21:29
    关注

    I use this 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
    

    Where there are 2 checkboxes as a kind of hack to always send the checkbox value, even if it's unchecked, based on this answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)