How to create pagination in eager loaded relationship in laravel
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.
The issue is that the paginate() method returns a LengthAwarePaginator instance, but when you eager load the relationship, it returns a Collection instance. To solve this issue, you can use the paginate() method on the relationship query instead of the eager loading query. Here's an updated version of the controller code: