Php Laravel:在刀片模板中循环使用动态路由参数

Php Laravel:在刀片模板中循环使用动态路由参数,php,laravel,Php,Laravel,我想在刀片模板中使用附加参数(本例中是一系列ID)循环命名路由。在代码中,我用星号突出显示了我要执行此操作的行。我的目的是在单击“下一步”按钮时访问下一页。下一页与前一页完全相同,只是有一张不同的问题卡。目前,点击按钮后页面不会改变 这是定义的路线: Route::get('/quiz/{id}', [QuestionController::class, 'show'])->name("question.show"); 这是生成视图的html刀片模板 `@forea

我想在刀片模板中使用附加参数(本例中是一系列ID)循环命名路由。在代码中,我用星号突出显示了我要执行此操作的行。我的目的是在单击“下一步”按钮时访问下一页。下一页与前一页完全相同,只是有一张不同的问题卡。目前,点击按钮后页面不会改变

这是定义的路线:

Route::get('/quiz/{id}', [QuestionController::class, 'show'])->name("question.show"); 
这是生成视图的html刀片模板

`@foreach ($questions as $question) 

                
                <div class="col-12 col-md-6 my-2 offset-md-3">

               


                    <div class="card shadow p-3 mb-5 border bg-warning rounded border">
                    
                        <h5 class="card-header"> {{ $question->category }} </h5> 
                    
                        <div class="card-body">
                            
                            <h6 class="card-subtitle mb-2 text-muted">{{ $question->text }}</h6>
                        
                            @foreach($question->options as $option)
                                <br>
                                <input  type='radio' name="{{ $question->id }}" value="{{ $option->id }}">     
                                 {{ $option->text}} <br>
                            @endforeach
                            
                        </div>
                         
                        **<a href = "{{route('question.show',['id' => $question])}}" class="btn btn-light btn-sm offset-2 offset-md-10" role="button">
                            Next**
                        </a>
                        
                    </div>
                
              

                </div>
        
            @endforeach`
`@foreach($questions作为$question)
{{$question->category}
{{$question->text}
@foreach($question->options as$option)

{{$option->text}}
@endforeach ** @endforeach`
您可以通过在foreach循环中使用索引来实现这一点

更改此选项:
@foreach($questions as$question)

对此:
@foreach($index=>$questions)

然后在锚定标记中执行以下操作:

<a href="{{route('question.show',['id' => $questions[$index + 1]])}}" 
   class="btn btn-light btn-sm offset-2 offset-md-10" role="button">

确保用if语句围绕按钮以捕捉最后一个问题

@if($index+1


编辑:缺少括号

您可以通过在foreach循环中使用索引来完成此操作

更改此选项:
@foreach($questions as$question)

对此:
@foreach($index=>$questions)

然后在锚定标记中执行以下操作:

<a href="{{route('question.show',['id' => $questions[$index + 1]])}}" 
   class="btn btn-light btn-sm offset-2 offset-md-10" role="button">

确保用if语句围绕按钮以捕捉最后一个问题

@if($index+1


编辑:缺少括号

谢谢@dengsauve。它抛出一个错误:“未定义的偏移量:1”您是否使用了if条件?是的,当我使用if条件时,“下一步按钮”消失。@ShreyaAgarwal单击“下一步”按钮时您是否获得了“未定义的偏移量:1”?是。然后我在按钮周围添加了if条件,这使得“下一步”按钮消失了。谢谢@dengsauve。它抛出一个错误:“未定义的偏移量:1”您是否使用了if条件?是的,当我使用if条件时,“下一步按钮”消失。@ShreyaAgarwal单击“下一步”按钮时您是否获得了“未定义的偏移量:1”?是。然后我在按钮周围添加了if条件,这使得“下一步”按钮消失了。