Laravel Eloquent whereBetween() Query Example

By Hardik Savani July 23, 2020 Category : 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 and laravel 8 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...