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); }
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.
Great Article https://www.tutsmake.com/laravel-where-multiple-conditions/