Laravel Multiple Where Conditions Example

Laravel Multiple Where Conditions Example

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.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

One reply to Laravel Multiple Where Conditions Example

Leave a Reply

Your email address will not be published. Required fields are marked *