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 Laravel password hash and check

How to create hash password in laravel 8 ?

January 26, 2022January 26, 2022

Laravel provides robust security features and one of them are hash password which is not decryptable. For hashing the password laravel use secure Bcrypt and Argon2 hashing for storing user passwords. Password encrypted with Bcrypt can not be decrypt since it uses key to generate the hashed string and irreversible…

Read More
Php Create Custom Directives in Laravel

How to create custom directive in Laravel 8 ?

November 11, 2021November 5, 2023

Laravel blade is powerful template engine which makes short tags directive and easy to reuse templates. Most of the templates restrict to use other language but blade gives more flexibility and we can use PHP as well in template. Directives start with prefix @ and accepts parameter. In this article…

Read More
Php Laravel count rows with example

How to use Laravel count rows with example ?

January 19, 2022November 9, 2023

MySQL count method gives the total number of rows in executed query, In Laravel we use eloquent or query builder to execute the query. In Laravel eloquent or query builder we use count method to count the rows. We can also count rows after executing the query using get() or…

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

  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version