Select the first 10 rows - Laravel Eloquent

Select the first 10 rows - Laravel Eloquent

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

Stefan Bogdanescu

Founder & Senior Architect

Select the First 10 Rows - Laravel Eloquent

Retrieving a specific subset of data from your database is one of the most frequent operations in a web application, yet the implementation varies significantly depending on whether you are using raw SQL or an ORM like Eloquent. In modern Laravel applications, relying on LIMIT clauses directly in raw queries should be avoided unless there is a compelling performance reason to do so. The framework provides a fluent interface that handles pagination logic and query building cleanly.

To achieve the goal of fetching the first 10 rows from your listings table, you start by defining the relationship between your model and the database. Your existing Listing model already extends Eloquent, which is correct. However, to make your code more expressive and easier to maintain, it is best practice to define the table name explicitly if it does not match the class name. If your class is named Listing and the table is listings, Laravel will infer this correctly by default. If you have renamed the model or the table, you would add the $table property:

class Listing extends Model
{
    protected $table = 'listings';
    
    // Optional: Define fillable fields for mass assignment protection
    protected $fillable = [
        'title', 'description', 'price'
    ];
}

Once the model is configured, the actual retrieval of data happens in your controller. You do not need to write a custom method within the model class to fetch rows; instead, you should keep the logic for querying data in the controller using Eloquent's query builder methods. Here is how you retrieve exactly 10 records:

use App\Models\Listing;

public function index()
{
    // Fetches only the first 10 records from the listings table
    $listings = Listing::limit(10)->get();

    return view('listings.index', compact('listings'));
}

In this example, limit(10) is a method provided by Eloquent that translates directly to a SQL LIMIT clause. This approach keeps your code readable and ensures you are not loading unnecessary data into memory. The get() method executes the query and returns a collection of models. You then pass this collection to the view using the compact function, which makes it available as a variable named $listings in your Blade template.

When displaying these results in your view, you can iterate over the collection efficiently. Since you are only fetching 10 rows, performance overhead is negligible for most applications. However, if your dataset grows significantly, consider implementing pagination using paginate(10) instead of limit(10). Pagination automatically handles splitting the results across multiple pages and provides helper methods to render navigation links.

$listings = Listing::paginate(10);
return view('listings.index', compact('listings'));

The Blade template remains consistent regardless of whether you use limit or paginate, though paginated results include extra metadata for the navigation bar. Always remember that querying without limits can be dangerous if a developer forgets to add pagination later, potentially causing high memory usage. By explicitly calling limit() or using paginate(), you enforce responsible data handling from the start.

For more advanced scenarios involving eager loading relationships or complex filtering before applying the limit, ensure you define your conditions before calling the limit method. For instance:

$listings = Listing::where('status', 'active')
    ->orderBy('created_at', 'desc')
    ->limit(10)
    ->get();

This sequence ensures that filtering and sorting occur before the row count is restricted, returning the most recent 10 active listings. This pattern of chaining query methods maintains code clarity and leverages the full power of Eloquent without requiring raw SQL statements.

Our network