Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to use limit query in Laravel Eloquent

How to use limit query in Laravel eloquent query with example

Aman Jain, February 5, 2022October 4, 2022

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. Laravel 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. It 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.

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

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 limit clause, 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

Related

Php Laravel laravellimitmysqlskiptake

Post navigation

Previous post
Next post

Related Posts

Php connect php to mysql

How to connect PHP to MySQL ?

September 27, 2021September 29, 2021

PHP is commonly used in the world of web. PHP has many built in tools to connect with different drivers and in this tutorial we are going to use MySQL driver to connection between PHP and MySQL. There is two to connect database in PHP 5 and later version. MySQLi…

Read More
Php Simplest Way to Use Roles and Permission in Laravel

Simplest Way to Use Roles and Permission in Laravel

August 23, 2022March 16, 2024

Roles and permission in laravel are used to provide roles based authentication and in this post we will learn simplest wat to use roles and permission in laravel. In our application we want features and modules on basis of roles or permissions so that we can restrict users to access…

Read More
Php How to assign or declare variable in laravel blade template

How to assign or declare variable in laravel blade template ?

November 8, 2022March 16, 2024

Laravel blade is a powerful templating engine that gives you a lot of control over your HTML. One thing you can declare variable in laravel blade template easily by using different methds. This can be useful if you want to reuse a piece of data in your template, or if…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version