获取从 Laravel 5.6 表单到控制器的 hasMany 关系的 ID
Getting ids for the hasMany relationship from Laravel 5.6 Form to the Controller
我正在尝试从 Laravel 编辑表单中获取单个问题的四个答案的 ID。
这是我的表单视图 blade 代码:
@php $i=0; @endphp
@foreach($question->answers as $answer)
@php $i++; @endphp
<div class="form-group">
<div class="col-lg-8">
{!!Form::label('Answeroption'.$i, 'Answeroption'.$i)!!}
{!! Form::text('answeropt'.$i,$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
</div>
</div>
@endforeach
我想在 QuestionController 的编辑方法中获取此 'id'=> $answer->id。这段代码位于问题编辑表单中,有 4 个答案选项。每个答案都存储在 answer table 中,其不同的 id 链接到主要问题 id。我想得到这个answerid,这样我就可以存储编辑过的答案。
我尝试执行 getIdAttribute 但它不起作用。
这是我的屏幕截图。每个选项在答案 table 中都有一个条目,question_id 作为 HasMany 关系链接到它。
编辑问题表单的屏幕截图
最好在标记中使用数组。然后您将拥有在 post 上引用的 ID。所以这可能看起来像:
{!! Form::text('answeropt['. $answer->id . ']',$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
然后在您的控制器中,您可以遍历数组并使用该 ID。所以这可能看起来像:
foreach($request->get('answeropt', []) as $answerId => $answer) {
// do things with the answer id and the answer text
}
我想这就是你想要的?请让我知道这是否清楚,或者您是否有其他意思。
我正在尝试从 Laravel 编辑表单中获取单个问题的四个答案的 ID。
这是我的表单视图 blade 代码:
@php $i=0; @endphp
@foreach($question->answers as $answer)
@php $i++; @endphp
<div class="form-group">
<div class="col-lg-8">
{!!Form::label('Answeroption'.$i, 'Answeroption'.$i)!!}
{!! Form::text('answeropt'.$i,$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
</div>
</div>
@endforeach
我想在 QuestionController 的编辑方法中获取此 'id'=> $answer->id。这段代码位于问题编辑表单中,有 4 个答案选项。每个答案都存储在 answer table 中,其不同的 id 链接到主要问题 id。我想得到这个answerid,这样我就可以存储编辑过的答案。
我尝试执行 getIdAttribute 但它不起作用。
这是我的屏幕截图。每个选项在答案 table 中都有一个条目,question_id 作为 HasMany 关系链接到它。
编辑问题表单的屏幕截图
最好在标记中使用数组。然后您将拥有在 post 上引用的 ID。所以这可能看起来像:
{!! Form::text('answeropt['. $answer->id . ']',$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
然后在您的控制器中,您可以遍历数组并使用该 ID。所以这可能看起来像:
foreach($request->get('answeropt', []) as $answerId => $answer) {
// do things with the answer id and the answer text
}
我想这就是你想要的?请让我知道这是否清楚,或者您是否有其他意思。