Spread the love

Limit clause is used to restrict the number of rows return in executed query. It’s quite useful when working with large data set since querying the large data creates performance issues so to overcome with the large dataset issue we use limit clause in a query. Limit clause accepts two inputs offset and count.

In this article we will learn to use limit in laravel eloquent and query builder. Laravel itself provide inbuilt method to limit the rows like in MySQL or SQL based database. As we described above limit accepts two parameters so in laravel also we can pass two parameters or one as well.

Limit method will give you the restricted number of rows defined in query. For example if you have 100 rows and you want the 10 rows in query then we need to pass 0, 10 to fetch the first 10 rows.

Laravel eloquent or query builder limt method do the same as limit MySQL function. Laravel limit accepts one parameter as count( number of count).

Here is the syntax

Model::limit(10);

//or

\DB::table('table_name')->limit(10);

In the above syntax we have used 1 examples to show possibilities to use limit function in laravel.

Result of above query will be:

select  * `table` limit 10

Also we can use offset function in laravel to define the starting point in query to limit. offset function accepts one parameter as integer.

Model::offset(10)->limit(10);

//or

\DB::table('table_name')->offset(10)->limit(10);

Result of above query:

select  * `table` limit 10 offset 10

Take and skip to limit the query in laravel

Take and skip methods are equivalent to offset and limit we can use any of them. Take and skip also accepts one parameter as integer so

Model::skip(10)->take(10);

//or

\DB::table('table_name')->skip(10)->take(10);

Result of above query:

select  * `table` limit 10 offset 10

As name implies that take and skip, they are doing the same take the number of rows and skip the number of rows.

Let’s understand laravel limit, offset, take and skip in eloquent query with example

Example 1 – Laravel Limit and offset function

So in this example i will use laravel query builder or eloquent Limit and offset .

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::offset(10)->limit(10); 
   }
}

Output will be :

select * from `articles` limit 10 offset 10

Example 2 – Laravel take and skip to limit the query

In this example i will use laravel query builder take and skip method to limit

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::take(10)->skip(10);
     
       \\OR
       DB::table('articles')->take(10)->skip(10)->first();
   }
}

In the above example we have used articles table and created a model in our laravel application. Then we imported the necessary classes and used the take and skip method.

Output will be :

select `aticles`.`*` from `articles` limit 10 offset 10

Also Read : Laravel Ajax pagination with search

Leave a Reply