Laravel Eloquent skip n, take all?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Eloquent Skip N Rows without Take All Hacks
Introduction: Laravel's Eloquent ORM provides various methods for querying data from the database. Some of these methods work well together, while others can be tricky to combine. One such example is the skip and take queries. In this article, we will explore a practical solution that enables you to skip nrows without using the take method or resorting to hacky tricks like specifying a large number for take.
1. Using Laravel's Query Scope
Query scopes offer an elegant way to reuse your complex queries with different parameters throughout your application. You can create a query scope called skipNRows without taking anything and apply it where needed. Here is the code:
```php
class User extends Model
{
/**
* Create a new database scope to skip N rows without using take all hacky trick.
*/
public function scopeSkipNRows($n)
{
return $this->where('id', '>=', \DB::raw('(SELECT MAX(id) FROM users LIMIT ' . ($n - 1) . ',1)'))->orderBy('id');
}
}
```
This method creates a scope called `skipNRows()`. It takes one parameter, $n, which represents the number of rows you want to skip. The query uses the MAX function and LIMIT to find and return all records greater than or equal to the last record before the skipped ones.
2. Using Laravel's Offset Query Builder Method
Another option for achieving this is using the offset() method in your Eloquent query builder call directly, which lets you specify an offset value. This will skip N rows and get the rest of the records without involving any hacks:
```php
User::skipNRows($n)->offset(($n - 1))->orderBy('id')->get();
```
This code first applies your custom `skipNRows()` scope to bypass the selected number of rows, then uses the offset method with an adjusted value to skip those specific rows. Finally, you can run the query and get all remaining records.
3. Using Laravel's Query Builder Method Chaining
If you prefer not using scopes or want more control over your queries, you can still achieve this functionality by chaining multiple Eloquent methods together. Here is one example:
```php
$users = User::where('id', '<', $skipped_rows_ids[0])->get();
foreach ($skipped_rows_ids as $row) {
array_push($users, User::findOrFail($row));
}
$rest_of_users = User::orderBy('id')->skip(count($skipped_rows_ids))->take(null)->get();
```
In this approach, you first get all users before the skipped ones. Then iterate through the skipped rows' IDs and append them to your collected users. Finally, you skip the previously selected records by using a take method with null as the parameter to fetch the remainder of the users without specifying a number.
Conclusion:
While Laravel provides various methods for querying data, it may not be apparent how to efficiently achieve specific results like skipping n rows without taking all subsequent ones. This blog post demonstrates practical solutions using different techniques such as scopes, offset method, and chaining multiple Eloquent methods together. Ultimately, the choice depends on your preferences and requirements. By following these examples or other similar approaches, you can easily achieve your desired results without resorting to any hacky tricks.