Loading image

Blogs /

Laravel 10 Eloquent whereBetween() Query: A Powerful Filtering Query

Laravel 10 Eloquent whereBetween() Query: A Powerful Filtering Query

  • showkat ali
  • 0 Comments
  • 974 View

The Laravel 10 Eloquent whereBetween() method is a convenient way to filter records that fall between two values. This can be useful for a variety of purposes, such as filtering products by price, users by age, or orders by date.

To use the whereBetween() method, simply pass it the name of the column you want to filter on and an array containing the two values between which you want to filter. For example, the following code would filter all users whose age column is between 18 and 25:

 

$users = User::whereBetween('age', [18, 25])->get();

You can also use the whereBetween() method to filter on date columns. For example, the following code would filter all orders that were placed between January 1, 2023 and March 31, 2023:

$orders = Order::whereBetween('created_at', ['2023-01-01', '2023-03-31'])->get();

The whereBetween() method can be used with any column type, including numbers, strings, and dates. It can also be used in conjunction with other Eloquent query methods, such as where(), orWhere(), and orderBy().

Here are some additional examples of how to use the whereBetween() method:

  • Filter all products whose price column is between $100 and $200:
$products = Product::whereBetween('price', [100, 200])->get();
  • Filter all posts that were published between January 1, 2023 and December 31, 2023:
$posts = Post::whereBetween('published_at', ['2023-01-01', '2023-12-31'])->get();
  • Filter all users who have logged in in the past 30 days:
$users = User::whereBetween('last_login_at', [Carbon::now()->subDays(30), Carbon::now()])->get();

The whereBetween() method is a powerful tool that can be used to filter records in a variety of ways. By using this method, you can write more efficient and concise Eloquent queries.

 

 

 

 

 
showkat ali Author

showkat ali

Greetings, I'm a passionate full-stack developer and entrepreneur based in Pakistan. I specialize in PHP, Laravel, React.js, Node.js, JavaScript, and Python. I own interviewsolutionshub.com, where I share tech tutorials, tips, and interview questions. I'm a firm believer in hard work and consistency. Welcome to interviewsolutionshub.com, your source for tech insights and career guidance

0 Comments

Post Comment

Recent Blogs

Recent posts form our Blog

Polymorphic Relationships in Laravel: A Comprehensive Guide with Example

Polymorphic Relationships in Laravel: A Comprehensive Guide with Example

showkat ali
/
Programming

Read More
How to Deploy a Next.js Application on AWS EC2 Using GitHub Actions

How to Deploy a Next.js Application on AWS EC2 Using GitHub Actions

showkat ali
/
Programming

Read More
How to Write an Appreciation Note to Your Friend

How to Write an Appreciation Note to Your Friend

Nasir Hussain
/
English

Read More
The Future of SEO: What Happens If ChatGPT Kills Search Engines?

The Future of SEO: What Happens If ChatGPT Kills Search Engines?

showkat ali
/
Programming

Read More
How to Make Your Website Faster: Speed and SEO Tips

How to Make Your Website Faster: Speed and SEO Tips

showkat ali
/
Technology

Read More
The Rise of AI: How Artificial Intelligence is Transforming Industries.

The Rise of AI: How Artificial Intelligence is Transforming Industries.

Sibgha jamil
/
Technology

Read More