Is there a way to "limit" the result with ELOQUENT ORM of Laravel?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Limiting Results with Eloquent ORM in Laravel: Techniques and Best Practices
When using the Laravel Framework's Eloquent Object Relational Mapper (ORM) for database interactions, it provides various methods to retrieve data efficiently. Sometimes, you may need to limit the results returned by a query. In this post, we will explore different techniques to do so and provide some best practices while working with Eloquent ORM.
Pagination
One of the most common ways to limit result sets is through pagination. Laravel comes with built-in support for pagination, which you can use on your database interactions as well. The `paginate()` method returns a Paginator instance containing the model instances. You can specify the number of results per page by passing it as an argument: $games = Game::paginate(20); // Display 20 games per page
Offset Limit Syntax
In certain cases, you may need to manually limit the results. You can use Laravel's query builder's offset and limit methods along with Eloquent models: $games = Game::orderBy('created_at', 'desc')->offset(30)->limit(15)->get(); // Retrieve 15 games starting from the 31st game
The offset starts from 0, so if you want to skip the first 30 results and display only the next 15, you'll have to set the offset at 30. This method is useful when you need to specify exact starting and ending points for a range of results.
Remember that this query will still load all the data from the database but display it in the specified order on your application. It may not be suitable if you work with large datasets or need to reduce memory usage.
Eager Loading & Relationships
Another method to limit results is by optimizing your database queries through eager loading and relationships. Eloquent provides a way to load associated models simultaneously, thereby reducing the number of queries required: $usersWithGames = User::with('games')->get(); // Load users with their games in one query
In this example, we load all users with their related games in a single query. This approach is more efficient and reduces the number of trips to the database.
Custom Scopes
If you require a more permanent solution for limiting results or need a specific logic, you can define custom scopes on your Eloquent model. A scope allows you to set up query conditions before retrieving data: public function scopeLatestGames($query) {
return $query->where('created_at', '>', now()->subDay());
}
In this example, we define a custom scope called `latestGames()` that filters games created within the past day. Now you can simply call:
$recentGames = Game::latestGames()->get(); // Retrieve only the latest games
This approach saves time and ensures better code maintainability since it is integrated into your model.