首页 > 解决方案 > 动态列名循环时如何在laravel中存储数据?

问题描述

这是我在数据库中存储数据的控制器代码

public function store(Request $request)
{
    $user = Auth::user();
    $input = $request->all();
    $mycheck= $input['mycheck'];
    //return $mycheck;

它返回

我需要此语句的位置应根据其索引自动选择问题并将数据保存在数据库中。// $user_response->question_{{$index}} = 1;

我的 CovidUserResponse 表是 ---

Schema::create('covid_user_responses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('question_1');
            $table->unsignedBigInteger('question_2');
            $table->unsignedBigInteger('question_3');
            $table->unsignedBigInteger('question_4');
            $table->unsignedBigInteger('question_5');
            $table->unsignedBigInteger('question_6');
            $table->unsignedBigInteger('question_7');
            $table->unsignedBigInteger('question_8');
            $table->unsignedBigInteger('question_9');
            $table->unsignedBigInteger('question_10');
            $table->boolean('status');
            $table->decimal('temperature', 5, 1);
            $table->date('recorded_date_time');
            $table->timestamps();

            $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        });

这是我获取输入值的表单代码——

<form action="{{ route('self-assessment.store') }}" method="post" enctype="multipart/form-data" autocomplete="off">
                     @csrf
                            @foreach($covidquestions as $covidquestion) 
                                @if($covidquestion->question_type == "radio")
                                <div class="form-group row">
                                    <label class="col-12" for="name">{{ $covidquestion->question_no }} {{ $covidquestion->question_title }}</label>
                                    <div class="col-12">
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="radio" name="mycheck[{{$covidquestion->id}}]" id="inlineRadio1" value="1">
                                            <label class="form-check-label" for="inlineRadio1">Yes</label>
                                        </div>
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="radio" name="mycheck[{{$covidquestion->id}}]" id="inlineRadio2" value="2">
                                            <label class="form-check-label" for="inlineRadio2">No</label>
                                        </div>
                                    </div>
                                </div>
                                @else
                                <div class="form-group row">
                                    <label class="col-12" for="temperature"> {{ $covidquestion->question_no }} {{ $covidquestion->question_title }}</label>
                                    <div class="col-12">
                                        <input type="text" class="form-control" id="temperature" name="temperature" placeholder="Enter Temperature.." required>
                                    </div>
                                </div> 
                                @endif
                            @endforeach
                                <div class="form-group row">
                                    <div class="col-12">
                                        <button type="submit" class="btn btn-primary">Save</button>  
                                    </div>
                                </div>
</form>

标签: arraysdatabaselaravelfor-loopdynamic

解决方案


您只是没有将正确的值附加到字符串:

foreach ($mycheck as $id => $value) {
    $user_response->setAttribute('question_'. $id, $value);
}

推荐阅读