nhayder
3 weeks ago

adding paginated collection to eloquent object

Posted 3 weeks ago by nhayder

this is getting complicated on my end so excuse the mess in the code, But i'm in a situation where i need to add a paginated list of articles to existing eloquent object. the code lives on the widget.php model as static method like this

    public static function getPageWidgets($id, $lang)
    {
    // this returns the widgets available on the page

        $pageWidget = Cache::rememberForever('page-'.$id, function () use ($id) {
        
            return Widget::with('buttons', 'content')

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

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

                    ->get();

        });

    //this returns the articles in a single widget

        $wArticles = Cache::rememberForever('wArticles-'.$id, function () use ($id, $lang) {
        
            return Article::where('category_id', '=', 1)
                    
                    ->orderBy('id', 'desc')

                    ->paginate(10);

        });
        
        return $pageWidget;
    }

i'm trying to add the $wArticles to $pageWidget object to look like this

// this is the widget
[
    {

    fullscreendisabled: false,
    showbuttonsdisabled: false,
    showarrowdisabled: false,
    disablecontentcount: true,
    disablecontentposter: false,
    .... etc ....

    buttons: [ ],
    content: [ ],
    articles: [ ] // <----- i need to add articles to it like this so i can loop through it in blade
    }

]

how can i add my articles collection as shown above

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