8 months ago

how to paginate a relationship inside eloquent model

Posted 8 months ago by nhayder

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')



        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

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