How can i use pagination laravel in this situation ?(Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id)

Viewed 1041

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id This is my Controller function

 public function viewQuestion($id){
    $tests=Test::find($id)->paginate(5);
    return view('admin.mocks.questions.all_question',compact('tests'));
}

This is my view

@foreach($tests->subjects as $key =>$subject)
<tr>
  <td>{{$subject->id}}</td>
  <td>{{$subject->subject_name}}</td>
</tr>
@endforeach
{{$tests->subjects->links }}
1 Answers

From the answer you gave me in your comments, to paginate subjects you first need to edit your controller code. First find a test with given ID, then load all test subjects and paginate them, instead of paginating single test result. Try this:

 public function viewQuestion($id){
    $test=Test::find($id);
    $subjects = $test->subjects()->paginate(5);
    return view('admin.mocks.questions.all_question',compact('subjects'));
 }

And then, alter your view:

@foreach($subjects as $key => $subject)
<tr>
  <td>{{$subject->id}}</td>
  <td>{{$subject->subject_name}}</td>
</tr>
@endforeach
{{$subjects->links }}

PS: Not tested, so let me know if you have any errors.

Related