Laravel Eloquent whereBetween() Query Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent Where Query Examples
- Laravel Multiple Where Condition Example
- How to Create and Use Query Scope in Laravel Eloquent
- How to use whereHas with orWhereHas in Laravel?
- How to use Union Query with Laravel Eloquent?
- Laravel WhereIn() and WhereNotIn() with Subquery Example
- Laravel 5.4 New Feature - Add Eloquent WhereKey Method Example
- Laravel Eloquent Where Like Query Example Tutorial
- Laravel Query Builder Where Exists Example