Foreach将数据发送到数据库两个foreach

时间:2017-08-17 09:51:07

标签: php laravel foreach

我在控制器中有这个代码:

QuestionGroup::create([
    'question_id' => $QuestionController->getQuestionIdAttribute(),
    'left_col_text' => $request['left_col_text'],
    'right_col_text' => $request['right_col_text']
]); 

foreach ($request['options'] as $key => $option) {
    $group = in_array($key, $request['group']);

    if($group==0){
        DragDropAnswer::create([
            'question_id' => $QuestionController->getQuestionIdAttribute(),
            'text' => $option,
            'group' => 0
        ]);  

    }
}

foreach ($request['right_options'] as $right_key => $right_option) {

    $right_group = in_array($right_key, $request['right_group']);

    if($right_group==1){
        DragDropAnswer::create([
            'question_id' => $QuestionController->getQuestionIdAttribute(),
            'text' => $right_option,
            'group' => 1
        ]); 
    }
}

观点:

<div class="form-group"
     id="option">

    <div class="col-md-4 col-md-offset-1">
        <input id="grouping_input" type="text" name="left_col_text" value=""
               class="form-control" placeholder="Left Group ">

        <div class="col-md-12"></div>
        <span class="help-block">
            <strong></strong>
        </span>

    </div>

    <div class="col-md-4 col-md-offset-1">
        <input  id="grouping_input" type="text" name="right_col_text" value=""
                class="form-control" placeholder="Right Group ">

        <div class="col-md-12"></div>
        <span class="help-block">
            <strong></strong>
        </span>

    </div>
</div>
<div  class="row">
    <div id="optionsForm" class="col-md-5 col-md-offset-1 ">
        @for($i = 1; $i<=4; $i++)
        <div class="form-group {{ $errors->has('options.'.$i) ? ' has-error': '' }}"
             id="option{{ $i }}">

            <div class="col-md-10">
                <input  type="hidden" name="group[{{ $i }}]" value="0"  />
                <input type="text" name="options[{{ $i }}]" value="{{ old('options.'.$i) }}"
                       class="form-control" placeholder="@lang('general.option') {{ $i }}">
                @if($errors->has('options.'.$i))
                <div class="col-md-12"></div>
                <span class="help-block">
                    <strong>{{ $errors->first('options.'.$i) }}</strong>
                </span>
                @endif 
            </div>
            <div class="col-md-1">
                <button type="button" value="{{ $i }}" class="btn btn-flat btn-default btn-sm"
                        id="delete_option" title="@lang('general.remove_option')">
                    <i class="fa fa-trash-o" aria-hidden="true"></i>
                </button>
            </div>

        </div>
        @endfor
    </div>

    <div class="col-md-5 " id="optionsForm1">
        @for($k = 1; $k<=4; $k++)
        <div class="form-group {{ $errors->has('options.'.$k) ? ' has-error': '' }}"
             id="match{{ $k }}">

            <div class="col-md-10  ">
                <input  type="hidden" name="right_group[{{ $k }}]" value="1"  />
                <input type="text" name="right_options[{{ $k }}]" value="{{ old('right_options.'.$k) }}"
                       class="form-control" placeholder="@lang('general.option') {{ $k }}">

                @if($errors->has('option.'.$k))
                <div class="col-md-12"></div>
                <span class="help-block">
                    <strong>{{ $errors->first('option.'.$k) }}</strong>
                </span>
                @endif
            </div>

            <div class="col-md-1">
                <button type="button" value="{{ $k }}" class="btn btn-flat btn-default btn-sm"
                        id="delete_match" title="@lang('general.remove_option')">
                    <i class="fa fa-trash-o" aria-hidden="true"></i>
                </button>
            </div>
        </div>
        @endfor
    </div>

我需要向数据库发送每个选项,但我只获得第一个foreach的四个选项,而另一个我总是得到第一个选项,所以对于right_options我只得到数据库的一个选项。

我已经看过dd();并且从视图中看到其中的4个所以我认为这意味着它不是视图而是控制器的问题。

有人可以帮我吗?

0 个答案:

没有答案