【问题标题】:How to store data in laravel when having loop for dynamic column names?动态列名循环时如何在laravel中存储数据?
【发布时间】:2021-03-01 14:42:27
【问题描述】:

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

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

返回

  • {

    • “23”:“1”,
    • “24”:“2”,
    • “25”:“1”,
    • “26”:“2”,
    • “27”:“1”,
    • “28”:“2”,
    • “29”:“1”,
    • “30”:“2”,
    • “31”:“1”,
    • “32”:“2”
  • }

      //return $input;
      $user_response = new CovidUserResponses();
      $user_response->user_id = $user->id;
      $user_response->recorded_date_time = Carbon::now();
      $user_response->temperature = $request->input('temperature');
      foreach($mycheck as $index => $mc)
      {
          //return $index;
          $user_response->question_$mc[$index] = $mc;
         // $user_response->question_1 = 1;
    
      } 
      return $user_response;
    

    }

我需要此语句的位置应根据其索引自动选择问题并将数据保存在数据库中。 // $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>

【问题讨论】:

  • 我认为您的 blade 文件中存在问题。请检查name=mycheck[{{$covidquestion-&gt;id}}] 是否创建了正确的索引。
  • 所以 32 是 question_32 ... 超过 10 个?

标签: arrays database laravel for-loop dynamic


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 2022-12-20
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多