Laravel Multiple Where Conditions Example - Yudhy Network

Share on:

Yudhy Network - Laravel multiple where conditions example. Here, you will learn how to use multiple where conditions with eloquent query builder and model in laravel

Laravel multiple where conditions example. In this tutorial, you will learn how to use multiple where clause with query in laravel web applications.

Sometimes, you may want to use fetch data with multiple where condition in laravel apps, This tutorial will show you various examples of how to use multiple where conditions with eloquent queries.

And also you can use where clause with laravel search query with multiple conditions, update query with multiple conditions, delete query with multiple conditions, and relationship with multiple conditions in laravel.

Let’s take a took at examples:

Example 1: Laravel multiple where clause

Multiple where conditions:

public function index()
{
    $users = User::where('status', 1)
                ->where('is_banned', 0)
                ->get(['name']);
   
    dd($users);
}
Recommended:- Laravel – where In Eloquent Query Example

Example 2: Laravel multiple where clause

Multiple condition in where clause:

public function index()
{
    $users = User::where([["status" => 1], ["is_banned" => 0]])
                ->get(['name']);
   
    dd($users);
}

Example 3: Laravel multiple where with Relationship

Go to your model and create scopes, like below:

public function scopeActive($query)

{

    return $query->where('active', '=', 1);

}



public function scopeThat($query)

{

    return $query->where('that', '=', 1);

}

Then call scops:

public function index()
{
    //Then call the scopes as given below

    $users = User::active()->that()->get();
}

Conclusion

In this laravel multiple where conditions example, you have learned how to use multiple where conditions with the query builder and eloquent model in laravel apps.

Recommended:- Laravel Eloquent whereRaw Query Example
Recommended:- Laravel whereIn, where Not In With SubQuery Example
Recommended:- Laravel where Like Query Example
Recommended:- Laravel where Between Dates Eloquent Query
Recommended:- Laravel where Not In Eloquent Query Example
Recommended:- Laravel WhereHas Eloquent Example
Recommended:- Laravel where Not Between Query Example
Recommended:- Laravel whereExists and where Not Exists Query Example
Recommended:- Laravel Where Null and Where Not Null Query
Recommended:- Laravel Group by Example
Recommended:- Laravel Order by Example

Please ask in the comments!

Suport : Cotekno.Com

comments powered by Disqus