2017-07-18 4 views
0

の前のレコードをbuttons代わりのhrefを使用して、次、前のレコードを取得する方法を、取得次とlaravel

例えば

<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を送信します。

+0

、あなたが理解してほしいですか? –

+0

@Sagar Gautam yes –

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

は、フォームに現在の質問のIDを送信したい

+0

あなたの助けてくれてありがとう、それは動作しませんでした。 –

+0

私はそれが動作するはずだと思う、なぜこれは動作していないのだろうか? –

+0

他の方法がありますか? –

関連する問題