2017-02-24 61 views
0

我有一個語音模型,可以保存所有新語音,每個語音都有名稱和語言字段。我的第二個模型是設備模型,我想在設備窗體視圖中將這些聲音填充爲複選框,並讓用戶選擇一個或多個聲音並將其保存到相關的設備中作爲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'); 
} 

機器人佛RM

<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> 

回答

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 

凡有2個複選框作爲一種黑客總是發送複選框值,即使它是否被選中,based on this answer

+0

感謝您的回覆@Thomas我解決了它:) –

+0

然後請將此答案標記爲解決方案 –

+0

我可以問你一個問題嗎?現在,當我創建新設備並檢查多個複選框時,我將存儲新的設備信息,但同時我想將** Device_id **和** Voice_id **保存到數據透視表中? –

相關問題