mercredi 5 juillet 2017

Creating multiple checkbox that needs to be send to database as 1 or 0

Creating question multiple checkbox and I need to send two possible cases 1 or 0 to the database but instead it always sends the last one I click as 1 all the others 0. So for ex. if if click check the first one and the last I want to send to the database both 1 and the other two unchecked 0.

My controller

    public function create($request){
    foreach ($request['options'] as $key => $option){
        Answer::create([
            'question_id' => $this->get(),
            'text' => $option,
            'correct' => $request['correct'] == $key ? 1 : 0
        ]);
    }
}

And my view

@for($i = 1; $i<=4; $i++)
                            <div class="form-group "
                                 id="option">
                                <div class="checkbox col-xs-2 control-label" style="margin-top: -2px">
                                    <label>

                           <input  id="cc" type="checkbox" name="correct" value=""   >

                                         <!--    {!! Form::hidden('correct',0) !!}
                                        {!! Form::checkbox('correct',1,false) !!} -->   


                                    </label>
                                </div>
                                <div class="col-xs-8">
                                    <input type="text" name="options[]" value=""
                                           class="form-control" placeholder="@lang('general.option') ">
                                    @if($errors->has('options.'.$i))
                                        <div class="col-xs-12"></div>
                                        <span class="help-block">
                                <strong></strong>
                            </span>
                                    @endif
                                </div>




Aucun commentaire:

Enregistrer un commentaire