Laravel 9 whereBetween Query Example

Websolutionstuff | Oct-14-2022 | Categories : Laravel

In this article, we will see laravel 9 whereBetween query example. The whereBetween() method is used to check value from two columns or a given range of the value. We will learn wherebetween queries in laravel 9. The whereBetween method verifies that a column's value is between two values.

So, let's see laravel 9 wherebetween query example.

BETWEEN Syntax of SQL

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

 

Example 1:

In this example, we will retrieve all records of employees with salaries between 10000 to 20000. 

SELECT * FROM employee
WHERE salary BETWEEN 10000 AND 20000;

 

whereBetween() Syntax of Laravel

 In the whereBetween() method first argument is the column name and the second argument is an array of range values.

whereBetween('Column Name', [Range of Value]);

 

 

Example 2:

In laravel 9, you can get data from the given range value using the below example query.

$employee = DB::table('employee')
           ->whereBetween('salary', [10000, 20000])
           ->get();

 

Example 3:

In this example, we will retrieve data from a given two date range using the whereBetween().

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Employee;
use Carbon\Carbon;
  
class RegisterController extends Controller
{
    public function index(Request $request)
    {
        $employee = Employee::whereBetween('joined_at',[$request->join_date,$request->resign_date])->get();
  
        dd($employee);
    }
}

 


You might also like:

Recommended Post
Featured Post
How to Create Payment Link in Stripe using API in Laravel 10
How to Create Payment Link in...

In today's digital age, the ability to facilitate online payments efficiently is crucial for businesses and develope...

Read More

Oct-09-2023

How To Encrypt And Decrypt String In Laravel 8
How To Encrypt And Decrypt Str...

In this example we will see how to encrypt and decrypt string in laravel 8 using crypt helper, As we all know laravel fr...

Read More

May-07-2021

Top 12 Tips To Improve React JS Performance In 2023
Top 12 Tips To Improve React J...

In the dynamic world of web development, staying ahead of the curve is essential, and in 2023, React JS continues to be...

Read More

Aug-28-2023

How To File Upload In Laravel 10 Example
How To File Upload In Laravel...

In this article, we will see how to file upload in laravel 10 examples. Here, we will learn about the laravel...

Read More

Mar-15-2023