【问题标题】:Inserting Multiple Checkbox value with laravel使用 laravel 插入多个复选框值
【发布时间】:2018-07-18 21:43:27
【问题描述】:

我正在尝试将具有多个复选框值的表单提交到我的 user_answer 列中

我的控制器

public function store(Request $request)
    {

        $input = $request->all();
        //dd($input);

        Answer::create($input);
        return back()->with('added', 'Answer has been submitted');
    }

我的观点

        @elseif($question->question_type == "$check_a" )
            <div>
            <span class="question-title">{!! $question->question !!}</span>
                <ul class="question-choices">
                    <li>
                        <label>
                            {!! Form::checkbox('user_answer[]', 'A'); !!} {!! $question->a !!}
                        </label>
                    </li>
                    <li>
                        <label>
                            {!! Form::checkbox('user_answer[]', 'B'); !!} {!! $question->b !!}
                        </label>
                    </li>
                {{--Show if the question has a value C --}}
                @if (!empty($question->c))
                    <li>
                        <label>
                            {!! Form::checkbox('user_answer[]', 'C'); !!} {!! $question->c !!}
                        </label>
                    </li>
                @else
                {{--Hide the checkbox from the Assessment--}}
                @endif

【问题讨论】:

  • 你遇到了什么错误?

标签: mysql laravel checkbox


【解决方案1】:

我觉得你应该试试这个

控制器

public function store(ProductRequest $request)
{

    $vehicleString = implode(",", $request->get('vehicle'));



    $status = $this->product->create([
        'name' => $request->get('name'),
        'cat_id' => $request->get('cat_id'),'vehicle' => $vehicleString




    ]);}

【讨论】:

  • @suzan 我没有收到来自 Laravel 的数组错误
【解决方案2】:

我最终走上了这条路并让它发挥了作用。

public function store(Request $request)
{    
    $input = $request->all();
    $answers = $request->input('user_answer');
if(is_array($answers)){ 
        foreach($answers as $answer)
        {
            DB::table('Answers')->insert(
                [
                'topic_id' => $request->input('topic_id'), 
                'user_id' => $request->input('user_id'), 
                'question_id' => $request->input('question_id'), 
                'user_answer' => $answer, 
                'answer' => $request->input('answer'),
                'created_at' => \Carbon\Carbon::now(), # \Datetime()
                'updated_at' => \Carbon\Carbon::now(), # \Datetime()
                ]
            );
        }
}else{
    echo "Not an array";
}
    return back()->with('added', 'Answer has been submitted');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多