kmangaraj's avatar

Getting Paginated Data for eager loaded query

Hi,

I am able to do this.


return $this->program
            ->with(['subjects' => function ($q) {
                $q->paginate(10);
            }])->findOrFail($id);

{
  "program": {
    "id": 1,
    "program_name": "Ut non qui.",
    "slug": "ut-non-qui",
    "program_description": "Consequatur recusandae iure maxime cupiditate.",
    "start_date": "2015-01-05 00:00:00",
    "to_date": "2012-07-11 00:00:00",
    "active": 0,
    "priority": 3,
    "created_at": "2015-10-16 11:03:37",
    "updated_at": "2015-10-16 11:03:37",
    "deleted_at": null,
    "subjects": [
      {
        "id": 6,
        "subject_name": "Enim qui ea.",
        "slug": null,
        "active": 0,
        "priority": 2,
        "program_id": 1,
        "created_at": "2015-10-19 07:10:07",
        "updated_at": "2015-10-19 07:10:07",
        "deleted_at": null,
        "image": null
      },
      {
        "id": 7,
        "subject_name": "Veritatis sit.",
        "slug": null,
        "active": 0,
        "priority": 10,
        "program_id": 1,
        "created_at": "2015-10-19 07:10:07",
        "updated_at": "2015-10-19 07:10:07",
        "deleted_at": null,
        "image": null
      },
      {
        "id": 8,
        "subject_name": "Quae qui.",
        "slug": null,
        "active": 1,
        "priority": 2,
        "program_id": 1,
        "created_at": "2015-10-19 07:10:07",
        "updated_at": "2015-10-19 07:10:07",
        "deleted_at": null,
        "image": null
      }
    ]
  }
}

And it successfully returns me data, but the problem is it doesnt return me the pagination links for the inner query. If i add ?page=2 in the url, it does show the next set of data, but no pagination links. Am i missing something??

0 likes
3 replies
phudev95's avatar

return Subject::where('progame_id', '=', $progame_id)->paginate(10);

Query Eloquent of your return Collection not instance LengthAwarePaginator object, so you cannot use param url page=2.

kmangaraj's avatar

@bestmomo 's answer did solve this particular problem, but had to modify it a bit when using with Fractal to get nested resources's paginated data. :)

Please or to participate in this conversation.