Laravel Eloquent skip n, take all?

Helen Che picture Helen Che · Dec 13, 2014 · Viewed 39.5k times · Source

I've noticed that in Laravel when chaining a skip() you must also use take() as well. I want to skip the first n rows but take the rest. The take method only allows integers how can I do this without resorting to some hacky trick such as specifying a large number for take?

Answer

Yousof K. picture Yousof K. · Dec 13, 2014

Basically, with every OFFSET, a LIMIT must be supplied for mysql to work. Therefore, there is no way to do this without sepcifying a limit. We need some php mojo to work here.

Let's say we have an Eloquent Class named Attendance. Here's what should work:

//Getting count
$count = Attendance::count();
$skip = 5;
$limit = $count - $skip; // the limit
$collection = Attendance::skip($skip)->take($limit)->get();