在laravel中获取下一个和上一个记录

在laravel中获取下一个和上一个记录,laravel,laravel-5,laravel-5.3,laravel-5.4,Laravel,Laravel 5,Laravel 5.3,Laravel 5.4,如何使用按钮而不是href获取下一条和上一条记录 比如说 <form method="post" action="/"> //Question and options will come here, //when click on next button next question should appear and // if I click on prev button it should goto prev question <butto

如何使用
按钮而不是
href
获取下一条和上一条记录

比如说

<form method="post" action="/">
    //Question and options will come here,
    //when click on next button next question should appear and 
    // if I click on prev button it should goto prev question
    <button type ="submit">Next Question
    <button type ="submit">Previous Question
</form>

//问题和选项将出现在这里,
//单击“下一步”按钮时,应出现下一个问题,并且
//如果我点击prev按钮,它应该转到prev问题
下一个问题
上一个问题
我的控制器

$questions = Question::find($qId);
$options = Question::find($qId)->options;
$previous = Question::where('id', '<', $questions->id)->max('id');
$next = Question::where('id', '>', $questions->id)->min('id');
return view('Pages/User/Questions/Question2')
        ->with('options',$options)
        ->with('questions',$questions)
        ->with('previous',Question::find($previous))
        ->with('next',Question::find($next));
$questions=Question::find($qId);
$options=Question::find($qId)->options;
$previous=Question::where('id','',$questions->id)->min('id');
返回视图(“页面/用户/问题/问题2”)
->带('options',$options)
->带('questions',$questions)
->带('previous',问题::find($previous))
->使用('next',问题::find($next));

现在,如何在“提交”按钮上发送下一个和上一个
id

您可以这样做:

您需要为按钮添加值,并且在提交值时也会提交。因此,您可以获得所需的记录

 <form method="post" action="/">

     // your question content

     <input name="qId" value="{{$question_id}}" hidden>

     <button type ="submit" name="option" value="0">Previous </button>
     <button type ="submit" name="option" value="1">Next </button>

 </form>

希望您理解,

您想用表单发送当前问题id吗?@Sagar Gautam yes必须使用隐藏输入字段有什么问题?@Sagar yes我可以这样做,但在按钮提交时获取下一个和上一个记录的问题。我添加了答案,请花一点时间查看是否有其他方法可以这样做?我以前也有类似的情况,并通过此解决了,其他人可能会给你最好的解决方案。祝你好运
public function getQuestion(Request $request){

    if($request->option==0){

         // Get previous record using $request->qId

    }else{

          // Get Next record using $request->qId

   }

  // Write code here to return data
}