8 months ago

stuck on how to paginate nested relationship

Posted 8 months ago by nhayder

it's been a couple of day trying to get my pagination code straight, thought i was told that laravel do not support nested relationship out of the box.

BUT i seriously hope that somebody will has been through this before and he made a fix for this case. because i have 0 solution so far.

Basically i need to paginate articles that are being loaded in widgets with nested relashioship.

this is my code without pagination.

    public static function getPageWidgetsWithArticles($id, $lang)
        $pageWidget = Cache::rememberForever('page-'.$id, function () use ($id,$lang) {
            return Widget::with(

                        ['articles' => function ($query) use ($lang) {

                            $query->where('language', '=', $lang);


                        'buttons', 'content', 'content.linkedPage', 'content.linkedArticle'

                    ->where('page_id', '=', $id)



        return $pageWidget;

the articles function should be paginated and by doing this

['articles' => function ($query) use ($lang) {

        $query->where('language', '=', $lang)->paginate(10);


laravel is not returning any paginated data ????

Has anybody been in this situation before ????? is there is a fix for this ???

Any ideas ???

Please sign in or create an account to participate in this conversation.