How To Create Multiple Where Clause Query Using Laravel Eloquent?

To create a multiple where clause query using Laravel Eloquent, you can chain the where method multiple times. Each where method call will add an additional condition to the query.

Here’s an example:

$users = User::where('age', '>', 18)
            ->where('gender', 'male')
            ->get();

In this example, the query will select all users where the age is greater than 18 and the gender is male.

You can chain as many where methods as needed to create more complex queries. Here’s another example:

$users = User::where('age', '>', 18)
            ->where('gender', 'male')
            ->orWhere('gender', 'other')
            ->where('created_at', '>', now()->subYear())
            ->get();

In this example, the query will select all users where the age is greater than 18 and the gender is either male or other, and the user was created within the last year.

You can also use the whereBetween method to check if a value is within a range:

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

This query will select all users where the age is between 18 and 25.

You can use other comparison operators like whereNotBetween, whereIn, whereNotIn, whereNull, whereNotNull, etc. depending on your requirements.

Remember to call the get method to execute the query and retrieve the results.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!