Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Can I use get and paginate together?

I have a case where I want to get the the data then extract information from it, but also paginate it.

In my case I want to pluck the id column from the query data

Instead of just doing 2 queries (for the get and pagination, though not big deal)

0 likes
7 replies
Sinnbeck's avatar

Whats the use case ? Sounds like it is just going to be really slow

1 like
Ligonsker's avatar

@Sinnbeck I want to pluck the ids column, and use the id array in another whereIn query

Sinnbeck's avatar

@Ligonsker So the pagination is on the second query?

$ids = User::where('foo', $bar)->pluck('id');
$posts = Post::whereIn('user_id', $ids)->paginate(20);
1 like
tykus's avatar

@Ligonsker a whereIn subquery can solve this; e.g.

User::whereIn('id', function($builder) {
    $builder->select('user_id')->from('posts');
})->paginate();

Now, you have only one query

1 like

Please or to participate in this conversation.