Using Laravel 12 you can use Eloquent’s `limit()` and `take()` methods to limit the number of records retrieved from the database.
Examples of Using Limit in Laravel 12 Eloquent
Using `limit()` Method
use App\Models\User; $users = User::limit(5)->get(); // Get only 5 users
Using `take()` Method (Alias of `limit()`)
$users = User::take(5)->get(); // Get only 5 users
Using `limit()` with `orderBy()`
$users = User::orderBy('id', 'desc')->limit(5)->get(); // Get last 5 users
Using `limit()` with `where()`
$users = User::where('status', 'active')->limit(5)->get();
Using `limit()` with `offset()` for Pagination
$users = User::orderBy('id')->offset(5)->limit(5)->get(); // Skip 5, get next 5
For proper pagination, use Laravel’s built-in pagination:
$users = User::paginate(5);
When to Use?
1. Fetching a limited number of records.
2. Implementing simple paginations.
3. Optimizing database queries for better performance.