Laravel Eloquent Limit() Query Builder Example

Laravel Limit() Query Example: In this tutorial, you will learn how to use eloquent limit Query in laravel application. The limit is used to get limited data from database.

If you are showing only 20 records to table then use limit or paginate the best option. If you need again 20 records skip first records then you need to add offset and then limit query. So let’s see how to work limit and offset in laravel applicaiton.

Example 1:

This example return 20 records of users table.

use App\Models\User;

public function index()
{
    $users = User::limit(20)->get();
}

Example 2:

This example return 20 records but skip first 20 records. These methods are functionally equivalent to the take and skip methods, respectively

use App\Models\User;

public function index()
{
    $users = User::offset(20)->limit(20)->get();
}

Example 3:

You may use the skip and take methods to limit the number of results returned from the query or to skip a given number of results in the query:

use App\Models\User;

public function index()
{
    $users = User::skip(20)->take(20)->get();
}

I hope these examples works for you.

Leave a Comment