justinl
justinl

Reputation: 10548

How to paginate in Laravel when using Modell::find(array())

I'm grabbing some data in Laravel using the find method and only grabbing the id's I want:

$my_ids = array(1,4,5,10); $results = Model::find($my_ids);

However, if I try to paginate via Model::find($my_ids)->paginate(10), it throws the error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate().

How can I query to only get specific model Id's back from the database while also using pagination?

Upvotes: 3

Views: 4486

Answers (2)

Edgar Orozco
Edgar Orozco

Reputation: 2782

I suggest use the eloquent query builder:

$results = Model::whereIn('id', $my_ids)->paginate(10);

Upvotes: 3

justinl
justinl

Reputation: 10548

Turns out I can use this syntax:

$results = Model::whereIn('id', $my_ids)->paginate(10);

Upvotes: 5

Related Questions