lundi 2 novembre 2015

Save array of checkboxes to database with laravel 5.1

I am new to Laravel and I am trying to save a form with inputs and an array of checkboxes were the values get populated from the database.

All I get is the word array or null in the DB column. All the other inputs are saved ok.

Here is my controller:

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('/');
    }
}

and here is my view:

{!! 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() !!}

I wonder if anyone could put an example of how to save the array of checkboxes for the faults into a single column.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire