nhayder's avatar
Level 13

how to paginate a relationship inside eloquent model

i'm trying to paginate list of articles inside my app news page withen main query like this

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

                        ['wsubcats','buttons', 'content', 'content.linkedPage', 'content.linkedArticle','articles' => function ($query) use ($lang) {

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

                        }]

                    )

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

                    ->orderBy('sorting', 'asc')

                    ->get();

        });

        return $pageWidget;
    }

in blade i need to show pagination btns so i'm doing this

{{ $widget->articles->links() }}

this is the error i'm getting

Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: /Applications/MAMP/htdocs/resources/views/includes/dynamicList.blade.php) (View: /Applications/MAMP/htdocs/resources/views/includes/dynamicList.blade.php) (View: /Applications/MAMP/htdocs/resources/views/includes/dynamicList.blade.php)

any idea on how to paginate this type of query

0 likes
12 replies
munazzil's avatar

Can you dd($widget) and check what was the output of the code?

assuming as like below pagination buttons should be,

      {{ $pageWidget->articles->links() }}
nhayder's avatar
Level 13

@sti3bas i've gone through your link, Unfortunately it's very difficult to understand the documentation for a beginner like me, Are you familiar for any tutorial that cover this type of pagination

nhayder's avatar
Level 13

just noticed that the code above is not returining pagionated data when i dd($widget->article);

So basically the pagionation is not taking place ????

Sti3bas's avatar

@nhayder I guess I misunderstood you. If you want to paginate the articles relationship, you should be able to do that with $widget->articles()->paginate(10).

1 like
nhayder's avatar
Level 13

i think now we are on the same page, But not sure how to implement your code with mine,

i have this

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

                        ['wsubcats','buttons', 'content', 'content.linkedPage', 'content.linkedArticle','articles' => function ($query) use ($lang) {

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

                        }]

                    )

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

                    ->orderBy('sorting', 'asc')

                    ->get();

        });

        return $pageWidget;
    }

but when i dd($widget->articles) i can't see any pagination is happening.

So how to implement your code with maine ??? any ideas

munazzil's avatar

Change as like below and check use $pageWidget instead of $widget,

   {{ $pageWidget->articles->links() }}

else

     {{ $Widget->links() }}
nhayder's avatar
Level 13

i'm getting same error for all cases, that because the links function is not available ???

Method Illuminate\Database\Eloquent\Collection::links does not exist
Snapey's avatar
Snapey
Best Answer
Level 122

You cannot paginate a nested relation.

You need to fetch the paginated collection as a separate variable and pass that to the view.

1 like
nhayder's avatar
Level 13

@snapey hey how are you, ...

Your comment is very clear, I was trying to understand if it's possible to paginate a nested relationship or not. will do a seperate variable for the paginated collection, that will definitely work.

So glad you showed up :-), Thanks

Please or to participate in this conversation.