Ronan Konishi
Ronan Konishi

Reputation: 51

How to set limit on a Collection in Laravel (Eloquent)

I want to query for some data that is of type Collection (according to Eloquent). When I query I want to set an upper bound. However, I cannot enforce this using

$data->skip(0)->take(5)->get();

nor by using

$data->offset(0)->limit(5)->get();

because $data is already a Collection and no longer a Builder type. How can I get the same functionality for Collection type?

Upvotes: 5

Views: 7871

Answers (1)

Don't Panic
Don't Panic

Reputation: 41820

You can get use the slice() method to get a subset of the Collection.

$data->slice(0, 5);

Upvotes: 12

Related Questions