0

I want the pagination to happen in eager loaded relation but when I am using it I am getting an error. Method Illuminate\Database\Eloquent\Collection::links does not exist. This only happens when I use {{ $SubjectReport->reports->links() }} in views rather than that everything is working fine as when I remove this {{ $SubjectReport->reports->links() }} the page loads with the number set in paginate method.

This is what I have tried so far

Here is my controller

$SubjectReport = Subject::with(['assesment.curriculumUnit',
    'reports' => function($q) {
    $q->orderBy('id','DESC')->paginate(2);
}])->where('id',2)->first();
 
return view('learner.assesment_reports',compact('SubjectReport'));

Here is my view


@foreach ($SubjectReport->reports as $key => $report)

@endforeach

{{ $SubjectReport->reports->links() }}

Baby Babai
  • 197
  • 1
  • 10

1 Answers1

1

Unfortunately, with doesn't work with pagination correctly. It's return only portion of data in Collection instead Paginator. The best way to paginate relation is:

$SubjectReport = Subject::with('assesment.curriculumUnit')->where('id',2)->first();
$SubjectReport->reports = $SubjectReport->reports()->orderBy('id','DESC')->paginate(2);
lezhni
  • 292
  • 2
  • 12