ItSolutionStuff.com

Laravel Eloquent whereBetween() Query Example

By Hardik Savani • April 16, 2024
Laravel

This post will give you example of laravel where between eloquent. This post will give you simple example of laravel eloquent wherebetween. i would like to share with you laravel eloquent wherebetween dates. you can see laravel wherebetween dates example. Here, Creating a basic example of laravel wherebetween date format.

whereBetween() will help you to getting data with between two dates or two values from database in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

In this example i will give you very simple example of how to use whereBetween in laravel application. you can easily use it with laravel 6 and laravel 7 application.

So, let's see bellow examples that will help you how to use whereBetween() eloquent query in laravel.

Example 1:

SQL Query:

select * from `users`

where `points` between ? and ?

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->whereBetween('points', [1, 100])

->get();

dd($users);

}

}

Example 2:

SQL Query:

select * from `users`

where `created_at` between ? and ?

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

use Carbon\Carbon;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$start = Carbon::now()->subDays(30);

$end = Carbon::now();

$users = User::select("*")

->whereBetween('created_at', [$start, $end])

->get();

dd($users);

}

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent Where Query Examples

Read Now →

Laravel Multiple Where Condition Example

Read Now →

How to Create and Use Query Scope in Laravel Eloquent

Read Now →

How to use whereHas with orWhereHas in Laravel?

Read Now →

How to use Union Query with Laravel Eloquent?

Read Now →

Laravel WhereIn() and WhereNotIn() with Subquery Example

Read Now →

Laravel 5.4 New Feature - Add Eloquent WhereKey Method Example

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

Laravel Query Builder Where Exists Example

Read Now →