Php 从Laravel 5.6表单到控制器获取hasMany关系的ID

Php 从Laravel 5.6表单到控制器获取hasMany关系的ID,php,laravel-5.6,laravelcollective,Php,Laravel 5.6,Laravelcollective,我试图从Laravel编辑表单中获取一个问题的四个答案的ID 以下是我的表单视图刀片代码: @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, '

我试图从Laravel编辑表单中获取一个问题的四个答案的ID

以下是我的表单视图刀片代码:

@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
@php$i=0@endphp
@foreach($问题->答案为$答案)
@php$i++@endphp
{!!Form::label('Answeroption'.$i,'Answeroption'.$i)
{!!Form::text('answeropt'.$i,$answer->answeropt,['class'=>'Form-control','id'=>$answer->id])
@endforeach
我想在QuestionController的编辑方法中获得这个'id'=>$answer->id。这段代码位于问题编辑表单中,包含4个答案选项。每个答案都存储在答案表中,其id与主要问题id链接。我想获取此答案id,以便存储编辑后的答案

我试图做getIdAttribute,但它不起作用

这是我的屏幕的截图。每个选项在答案表中都有一个条目,其中的问题id链接到该条目,因为它有许多关系

编辑问题表单的屏幕截图


最好在标记中使用数组。然后,你将有身份证后参考。所以这可能看起来像:

{!! Form::text('answeropt['. $answer->id . ']',$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
foreach($request->get('answeropt', []) as $answerId => $answer) {
    // do things with the answer id and the answer text
}
然后在控制器中,您可以在阵列上循环并利用id。这样看起来可能类似于:

{!! Form::text('answeropt['. $answer->id . ']',$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
foreach($request->get('answeropt', []) as $answerId => $answer) {
    // do things with the answer id and the answer text
}

我想这就是你想要的?请让我知道这是否清楚,或者你是否有其他意思。

是的,谢谢布莱恩!这是非常有用的。