Laravel - Eloquent or Fluent random row

Laravel - Eloquent or Fluent random row

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel - Eloquent or Fluent random row

Selecting a random record in Laravel without first calculating the total count of rows is a common requirement when performance matters. The naive approach often involves chaining count() and take(1) with skip(), but this forces the database to iterate through every single record just to determine how many exist before selecting one. That operation is O(n) in complexity, meaning it scales linearly as your dataset grows, which can introduce significant latency on large tables.

Laravel's Eloquent ORM and Query Builder provide a more efficient alternative by delegating the randomness logic directly to the database engine. The core mechanism relies on the orderBy method combined with a random function specific to your database driver.

use App\Models\Post;

$post = Post::inRandomOrder()->first();

This single line achieves what you need without an explicit count query. Under the hood, Eloquent translates inRandomOrder() into a SELECT * FROM posts ORDER BY RAND() LIMIT 1 statement for MySQL and PostgreSQL. The database engine handles the randomization during the scan, returning only one result immediately. This is generally much faster than counting first because the database optimizer can often short-circuit the operation or utilize specific indexing strategies depending on the SQL dialect.

However, relying on ORDER BY RAND() is not universally efficient across all database systems. MySQL has historically supported this well due to its implementation of RAND(), but it still scans the entire table unless there are no other constraints. If you are using a high-volume dataset, even a single full-table scan might be too heavy for an API endpoint that expects sub-millisecond latency.

For PostgreSQL and SQLite, the syntax differs slightly. While Eloquent's inRandomOrder() is abstract enough to work with most drivers out of the box, understanding the underlying SQL helps when debugging slow queries. In some versions of MySQL, using a user-defined variable approach like ORDER BY RAND() + (SELECT COUNT(*) FROM ...) was once considered for performance, but modern optimizers handle simple ORDER BY RAND() reasonably well for moderate table sizes.

If you find that inRandomOrder() is too slow for your specific use case, consider whether you truly need a purely random selection or if a different distribution suffices. For instance, if you are building a "random of the day" feature, pre-calculating a list of candidates and storing their status in a cache might be more scalable than querying the database on every request. You could use Laravel's cache facade to store a randomized list generated once per hour or day, effectively shifting the computational cost from the read path to an infrequent write path.

Another optimization strategy involves filtering before randomizing. If you only need a random post from a specific category, always include that filter in your query:

$post = Post::where('category', 'tech')->inRandomOrder()->first();

Failing to add necessary scopes or conditions before calling inRandomOrder() can lead to unintended results where unrelated records are returned. Always verify that your query returns the expected subset of data.

In summary, using inRandomOrder() is the standard, clean way to fetch a random row in Laravel without triggering an expensive count query. It leverages the database engine's capabilities directly, keeping your PHP code simple and your SQL efficient for most production scenarios. If performance becomes a bottleneck as your dataset expands, evaluate whether caching strategies or application-level randomization would better suit your architecture than raw database queries.

Our network