使用laravel 5.1将数组复选框保存到数据库

时间:2015-11-02 10:31:55

标签: arrays forms laravel laravel-5.1

我是Laravel的新手,我正在尝试使用输入保存一个表单,并且从数据库中填充了一系列复选框。

我得到的只是DB列中的单词array或null。所有其他输入都保存好了。

这是我的控制器:

public function storePC()
{               
    $rules = array(
        'brand'       => 'required',
        'touchscreen'       => 'required',
        'processor'       => 'required',            
        'condition'       => 'required',
        'faults'          => 'required',
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if ($validator->fails()) {
        return Redirect::to('/quote')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } else {
        // store
        $service = new Service;
        $service->brand       = Input::get('brand');
        $service->touchscreen      = Input::get('touchscreen');
        $service->processor      = Input::get('processor');
        $service->condition      = Input::get('condition');                 
        $service['faults'] = json_encode($quote['faults']);
        $service->save();

        // redirect
        Session::flash('message', 'Service Successfully created!');
        return Redirect::to('/');
    }
}

这是我的观点:

{!! Form::open(array('url' => '/store-service')) !!}

                <div class="form-group">
                  {!! Form::Label('brand', 'Brand:') !!}
                  <select class="form-control" name="brand" id="brand">
                    <option value="" selected="selected">Please select a Brand</option>
                    @foreach($brands as $brand)
                      <option value="{{$brand->brand}}">{{$brand->brand}}</option>
                    @endforeach
                  </select>
                </div>

                <div class="form-group">
                  {!! Form::Label('webcam', 'Does your device have an inbuilt Webcam?') !!}
                  <select class="form-control" name="webcam" id="webcam">
                      <option value="" selected="selected">Please select</option>                     
                      <option value="yes">Yes</option>
                      <option value="no">No</option>
                  </select>
                </div>                  

                <div id="second-step">
                    <div class="form-group">
                      {!! Form::Label('touchscreen', 'Does your device have a Touchscreen?') !!}
                      <select class="form-control" name="touchscreen" id="touchscreen">
                          <option value="" selected="selected">Please select</option>                         
                          <option value="yes">Yes</option>
                          <option value="no">No</option>
                      </select>
                    </div>

                    <div class="form-group">
                      {!! Form::Label('processor', 'Processor:') !!}
                      <select class="form-control" name="processor" id="processor">
                        <option value="" selected="selected">Please select a Processor</option>
                        @foreach($processors as $processor)
                          <option value="{{$processor->processor}}">{{$processor->processor}}</option>
                        @endforeach
                      </select>
                    </div>

                    <div class="form-group">
                      {!! Form::Label('condition', 'Is your laptop fully working and in good condition?') !!}
                      <select class="form-control" name="condition" id="condition">
                          <option value="yes" selected="selected">Yes</option>                            
                          <option value="no">No</option>
                      </select>
                    </div>
                    <div id="third-step">
                        <div class="col-md-12">
                            <div class="span8 mutli-column">
                                <div class="row-fluid">
                                    <div class="form-group">                                    
                                        @foreach($faults as $fault)
                                        <div class="col-md-3">
                                            <span><i class="fa fa-question-circle" rel="popover" data-content="{{$fault->fault_tooltip}}"></i></span>
                                            <label class="checkbox-inline"><input id="faults" type="checkbox" name="faults[]" value="{{$fault->fault}}">{{$fault->fault}}</label>
                                        </div>
                                        @endforeach
                                    </div>                  
                                </div>                              
                            </div>
                        </div>
                    </div>
                </div>
            {!! Form::submit('Submit', array('class' => 'btn-lg btn-danger')) !!}

            {!! Form::close() !!}

我想知道是否有人可以举例说明如何将故障的复选框数组保存到单个列中。

由于

1 个答案:

答案 0 :(得分:1)

我希望这有帮助

使用

$service->faults = json_encode($quote['faults']);

而不是

$service['faults'] = json_encode($quote['faults']);