arrays - 动态列名循环时如何在laravel中存储数据?
问题描述
这是我在数据库中存储数据的控制器代码
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>
解决方案
您只是没有将正确的值附加到字符串:
foreach ($mycheck as $id => $value) {
$user_response->setAttribute('question_'. $id, $value);
}
推荐阅读
- python - cvxpy 上的 MOSEK 求解器:TypeError
- angular - Chart.js 雷达图的不同 scaleLine 颜色(角度)
- coq - Coq 中 Prop、Set 和 Type_i 的基数
- php - 从 json 响应中回显某些变量
- php - 在 PHP 中使用 Microsoft Graph 更新电子邮件的主题
- javascript - 如何使基于类的自定义元素无副作用,以便 webpack 仅捆绑显式导入的组件
- javascript - 如果事件发生在两个日期jquery之间,如何突出显示日历的日期?
- bash - 复制 bash 脚本的输出
- java - 单元测试 Java API/Http 请求
- angular6 - 用于下载媒体文件和调度进度的 NgRX 效果;如何处理流和节流