Laravel Multiple Where Condition Clause Query

Laravel Multiple Where Condition Clause Query

To create multiple where clause condition query using laravel eloquent. In this tutorial, you will learn how to create and use multiple where clause query using laravel eloquent

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

To create multiple where clause query using laravel eloquent, 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, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

One reply to Laravel Multiple Where Condition Clause Query

Leave a Reply

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