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

时间:2017-07-18 09:24:22

标签: laravel laravel-5 laravel-5.3 laravel-5.4

如何使用buttons代替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>

我的控制器

$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));

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

1 个答案:

答案 0 :(得分:0)

你可以这样做:

您需要为按钮添加值,并且还将提交提交值。所以,你可以得到想要的记录。

 <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>

现在,您必须使用选项值

来检查单击了哪个按钮
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
}

希望你明白,