【问题标题】:How to populate multiple Checkboxes from to Database?如何将多个复选框填充到数据库?
【发布时间】:2017-07-15 20:15:38
【问题描述】:

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

【问题讨论】:

    标签: php json laravel eloquent blade


    【解决方案1】:

    我用这个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

    【讨论】:

    • 我能问你一个问题吗?现在,当我创建新设备并检查多个复选框时,我正在存储新设备信息,但同时我想将 Device_idVoice_id 保存到数据透视表?
    • 您能在一个新问题中提出这个问题吗?我需要更多解释:)
    • 当然,我会和你分享链接:)
    猜你喜欢
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 2017-10-11
    相关资源
    最近更新 更多