Laravel Eloquent skip n, take all?
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();
If you're using MySQL and you don't want to have 2 queries to get the max rows, you may use the PHP maximum INT value as take()
parameter e.g. take(PHP_INT_MAX)
.
It's because Laravel Query Grammar casts the limit to integer. So you can't use a larger number than that.
From MySQL documentation on LIMIT:
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter.
For example:
\App\User::skip(10)->take(PHP_INT_MAX)->get();