2017-07-22 2 views
1

私の場合、それぞれのオプションで質問を1つずつ取り出し、オプションIDを回答テーブルに保存する必要があります。オプションIDをanswersテーブルに保存します。ここでレコードを行単位で取得し、その値を保存する

は私のコントローラ

public function getQuestion(Request $request,$qId) 
{ 
    $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)); 
} 

マイルート

Route::get('/question/{qId}', [ 
     'uses' => 'customers\[email protected]', 
     'as' => 'question' 
]); 

は、私がいない行で、レコードの行をfecthだけにできることができますようなコードがどのように見えるかである私の見解

@foreach  
    <h3>{{$questions->question_name}}</h3> 
     @foreach($options as $option) 
      <div class="checkbox"> 
     <label><input type="checkbox" name="option">{{$option->option}}</label> 
     @endforeach 
@endforeach 
    @if($previous) 
    <a href="{{route('question',['qId' => $previous->id])}}">Previous</a> 
    @endif 
    @if($next) 
    <a href="{{route('question',['qId' => $next->id])}}" >Next</a> 
    @endif 

ですフォームには存在しないので、オプションテーブルのオプションIDを保存してください。

+0

なぜフォームを使用しないのですか? –

+0

@ Mohammad b私はフォームを試しましたが、レコードを1つずつ取り出すことができませんでした。 –

+0

データベースに回答をどのように保存したいのですか? –

答えて

0

あなたは、このようなビューに変更する必要があります。この方法では、オプションのIDを選択

@foreach(put here main foreach loop)  
    <h3>{{$questions->question_name}}</h3> 
    <form action="storeRoute"> 
     @foreach($options as $option) 
      <div class="checkbox"> 
     <label><input type="checkbox" name="option[]" value="{{ $option->id }}">{{$option->option}}</label> 
     @endforeach 
     <button type="submit">Submit</button> 
    </form> 
@endforeach 
    @if($previous) 
    <a href="{{route('question',['qId' => $previous->id])}}">Previous</a> 
    @endif 
    @if($next) 
    <a href="{{route('question',['qId' => $next->id])}}" >Next</a> 
    @endif 

をオプションの配列

としてstoreRouteに送信し、あなたが選択を取得するために、コントローラでのforeachを使用することができますopstions値

選択したすべてのオプションを1つずつ取得する場合は、次のコードを使用してください。

foreach ($request->option as $opt){ 
    //code to store $opt as question answer 
} 

または選択したすべてのオプションを保存できる場合$request->option

+0

あなたの答えに感謝しますが、私は次の(ハイパーリンク)ボタンでルートを保存するためにオプション配列を送信する必要があります。可能であれば、送信ボタンを使用すると3つのボタンが表示されません。 –

+0

@ ManjunathAMコントローラのリストアオプションが次のページのURLにリダイレクトされた後、またはjqueryを使用して次のページに進むなど、さまざまな方法があります。 –

関連する問題