Write laravel example to set limit and offset in Laravel PHP

Published January 17, 2022

Using the OFFSET value, you can specify which row to start when you are retrieving a keyword. The OFFSET value is usually used together with the LIMIT keyword. This post presents an example of how to set the limit and offset in a Laravel project.

 

How to set Limit and Offset in Laravel PHP

In this example, we will use the setLimit and offset functions in Laravel to select a specific set of data from a MySQL database. Let's go through the steps below:

Step 1:Create a new Laravel project in your Composer command prompt using the  command below

composer create-project --prefer-dist laravel/laravel LimitOffset

 

 

How to set Limit and Offset in Laravel PHP

 

A new project will be created in your htdocs folder.

Step 2: Use the SQL query below to create a new database where your data will be stored for future retrieval

CREATE DATABASE blog;

 

Create a database table called users with id, name, and address columns and populate it with sample data

How to set Limit and Offset in Laravel PHP 2

 

Now that our database is set up, let's link it to our Laravel application.

Step 3: Create a connection between your Laravel application and the MySQL database by configuring your .env file. To do this, set your DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, and DB_PASSWORD

 

How to set Limit and Offset in Laravel PHP 3

 

Our database connection is set. Now let’s proceed to generating migration.

Step 4: Create a userController.php using the following command

php artisan make:controller UserController;

 

 

How to set Limit and Offset in Laravel PHP 4

Open the UserController.php and paste the code below

<?php

 

namespace App\Http\Controllers;

 

use Illuminate\Http\Request;

 

//import database class

use Illuminate\Support\Facades\DB;

 

class UserController extends Controller

{

//

function index()

{

        return DB::select("SELECT name, email, address FROM  users ORDER BY id  LIMIT 5 OFFSET 3");

}

}

 

 

In this code, we have implemented the set Limit and offset in SQL Query. This query will select data from our users table.

Step 5: Let’s create a route to display our view.  Open, webb.php and paste the code below

 

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

 

Route::get('/', function () {

return view('welcome');

});

Route::get('users', [UserController::class,'index']);

 

 

How to set Limit and Offset in Laravel PHP 5

 

We are done. Run the command php artisan serve to obtain the application url.

 

Paste the url http://127.0.0.1:8000/users on your browser.

How to set Limit and Offset in Laravel PHP 6

 

In this laravel tutorial we covered write a query to set limit and offset for the where condition

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

677 Views