New in Laravel 10.47: Eloquent whereAll() and whereAny() - TechvBlogs

New in Laravel 10.47: Eloquent whereAll() and whereAny()

Laravel 10.47 introduces whereAny() and whereAll(), simplifying complex Eloquent queries for cleaner, more readable code.

Suresh Ramani - Author - TechvBlogs
Suresh Ramani

4 months ago

TechvBlogs - Google News

Laravel's Eloquent query builder empowers developers to interact with databases efficiently. Version 10.47 introduces two valuable methods, whereAny() and whereAll(), that simplify constructing complex queries, enhancing code readability and maintainability.

New in Laravel 10.47: Eloquent whereAll() and whereAny()

Understanding the Need

Before these additions, crafting queries involving multiple conditions often necessitated nested where clauses. While functional, this approach could lead to verbose and less readable code. Consider searching for users where the name, email, or phone number contains "test":

$search = 'test';
$users = User::where(function ($query) use ($search) {
    $query->where('name', 'LIKE', "%$search%")
        ->orWhere('email', 'LIKE', "%$search%")
        ->orWhere('phone_number', 'LIKE', "%$search%");

This code, while achieving the desired outcome, can become cumbersome and challenging to manage as the number of conditions and columns increase.

Introducing whereAny() and whereAll()

whereAny() and whereAll() offer a more streamlined and intuitive approach to achieve similar functionality as nested where clauses.

  • whereAny(): This method enables you to specify multiple columns, an operator, and a search value. It returns results where at least one of the specified columns matches the given condition.
    $search = 'test';
    $users = User::whereAny(['name', 'email', 'phone_number'], 'LIKE', "%$search%")->get();

    This code accomplishes the same task as the previous example but with a more concise and readable structure.

  • whereAll(): In contrast, whereAll() ensures that all the specified columns meet the defined condition.
    $data = [
        'name' => 'John Doe',
        'email' => '[email protected]',
    $users = User::whereAll($data)->get();

    This code retrieves users where both the name and email columns match the corresponding values in the $data array.

Benefits of Using whereAny() and whereAll()

  • Enhanced Readability: These methods significantly improve code readability by eliminating nested clauses, making complex queries easier to understand and maintain.
  • Reduced Verbosity: They condense code, leading to cleaner and more concise queries.
  • Intuitive Syntax: The syntax is straightforward and aligns with existing query builder methods, making it easy to learn and adopt.


whereAny() and whereAll() are valuable additions to Laravel's Eloquent query builder. They empower developers to construct complex queries with improved readability, reduced verbosity, and an intuitive syntax, ultimately enhancing the development experience and code maintainability. As you upgrade to Laravel 10.47 and beyond, consider incorporating these methods to streamline your database interactions and write more efficient and elegant queries.

Comments (0)


Note: All Input Fields are required.