Laravel Eloquent skip n, take all?

Laravel Eloquent skip n, take all?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel Eloquent skip n, take all?

When you are paginating a large dataset in Laravel, skip() and take() are your go-to methods for controlling how many rows the query executes against the database. You might encounter a scenario where you need to discard the first $n$ records but return every single record that follows them. The immediate reaction is often to chain a take() method with a massive integer, like ->skip(10)->take($total - 10). While this works logically within the context of SQL, it introduces unnecessary overhead and obscures your intent.

In standard SQL, there is no direct equivalent to "skip $n$ and return the rest." Most relational databases handle this differently depending on their specific syntax. For instance, MySQL supports LIMIT offset, count, but setting the count to a sentinel value like the maximum integer often feels like a hack rather than a clean solution. PostgreSQL offers a cleaner alternative with LIMIT -1 after an OFFSET, which explicitly tells the engine to return all remaining rows.

Laravel's Eloquent ORM abstracts this complexity away by default, but it enforces a strict pattern: you cannot call skip() without eventually calling take(). This constraint exists because Eloquent needs to know whether the query is intended for pagination or if it should behave like a simple "get all" operation once the offset is applied. If you omit take(), Eloquent might attempt to calculate the total number of rows matching your criteria, potentially triggering expensive COUNT(*) operations in the database that you didn't request.

To solve this elegantly without relying on arbitrary large numbers, you should leverage Laravel's "raw query" capabilities or specific builder methods that bypass the pagination assumption while maintaining type safety. The most robust approach involves using the cursor() method combined with a custom logic to handle the offset, but that adds complexity for simple cases. A more direct solution is to use the underlying query builder's ability to set limits dynamically or execute a raw SQL statement that mirrors the database's native capabilities.

If you are working with MySQL 8.0 or later, you can construct a query that effectively mimics the OFFSET ... LIMIT -1 behavior using Eloquent's raw methods. You can define a scope within your model to handle this pattern cleanly:

public function scopeSkipAll($query, $offset)
{
    return $query->orderBy('id')->skip($offset);
}

However, simply calling skip() without take() will still default to fetching all rows in many database configurations if the limit isn't explicitly set. To truly enforce "skip $n$ and take all," you need to ensure the database driver interprets your intent correctly. In MySQL, you can achieve this by setting the limit to a very high number that exceeds your dataset size, which is effectively what take() does, but it feels inelegant.

A better pattern involves checking if the query builder has been paginated. If you are using Laravel's paginator facade, you might want to avoid pagination entirely for this specific use case. Instead, consider building a raw query string that includes the offset and a limit of -1 (if supported) or simply omitting the limit clause after the offset:

// MySQL example using raw SQL within Eloquent
public static function getRecordsAfterOffset($model, $offset)
{
    return DB::table($model->getTable())
        ->orderBy('id')
        ->skip($offset)
        ->limit(-1); // MySQL supports this in some contexts, or just omit limit
}

Note that relying on limit(-1) is database-specific. In pure Eloquent without raw SQL injection risks, the safest bet is often to accept that you must specify a limit if you want to avoid the implicit count query. However, if your goal is simply to skip rows and return the rest for an API endpoint, using cursor() with a custom offset logic in your application layer can be more efficient than forcing a database-level hack.

The trade-off here lies between database performance and code clarity. Using a massive integer for take() works but signals that you are working around a limitation rather than leveraging the tool's full capabilities. Understanding how your specific database driver handles LIMIT clauses is essential. If you are on PostgreSQL, setting the limit to -1 is standard practice and supported natively by Eloquent when using raw queries. For MySQL, omitting the limit clause after an offset might still return all rows depending on the version and configuration, but explicitly setting a high number ensures consistency.

Ultimately, the decision depends on whether you prioritize strict type safety in your Eloquent chain or flexibility in your raw SQL construction. If you are building a public API where performance is critical and datasets are large, optimizing this query pattern can save significant database load compared to fetching unnecessary rows and filtering them in PHP. Always remember to index your ORDER BY columns, as skipping rows without an index forces the database to scan sequentially from the start, negating any benefit of the offset optimization.

Our network