ahmadbadpey's avatar

prepend to paginator fo dingo api in laravel

I'm using Dingo laravel api package. as you know this package has many features include pagination results.

Suppose in a controller method I want to return paginated Post of my app. like this :

    public function category_posts ($cat_id)
    {
        $posts =
            Post::where('active',true)
                ->whereHas(
                    'categories', function ($query) use ($cat_id) {
                    $query->where('categories.cat_id', $cat_id);
                })
                ->paginate(10);


        return $this->paginator($posts, new PostTransformer());
    }

As you see I first select all posts and then paginate them and in each page 10 posts is returned.

But some posts are pinned.means that have a pin field that is true.

Now I want, when user requests posts, if current page is 1 , I select that pin posts and prepend to $posts collection. But I do not know how can I do?

Update :

each category can have some posts and on the other hand each post is belongs to many category .in this case I have manyToMany relation between post and category . in addition to, when user requested a category posts I want to prepend latest pin post that is assigned to that category

0 likes
1 reply

Please or to participate in this conversation.