The database query builder in Laravel provides a simple, intuitive interface for generating and performing database queries. It works with every one of Laravel's supported database systems and may be used to conduct most operations on the database in your application.

To query a database, Laravel provides easy eloquent, and query builder methods. The where() method is the most basic clause in a database query. Every query nearly always requires a where clause.

You will learn how to use numerous where conditions in Laravel eloquent and query builder in this comprehensive tutorial. Numerous where conditions can be used to filter multiple columns into a database.

Let's look at the where() clause syntax in Laravel first:
->where('column_name', 'operator', 'value')

If you want to execute the where clause on multiple columns, do it as follows:

->where('column_name', 'operator', 'value')
->where('another_column', 'operator', 'value')

You can also use the where clause to pass an array of conditions.

->where([
    ['column_name', 'operator', 'value'],
    ['another_column', 'operator', 'value']
])

Both queries result in the same MySQL query:

SELECT * FROM `table_name` WHERE column_name = value AND another_column = value

Let's have a look at an example query:

1. Example

/**
 * Return users
 *
 * @return void
 */
public function index()
{
    $users = User::select('*')
        ->where('is_active', '=', 1)
        ->where('is_delete', '=', 0)
        ->get();
   
    dd($users);
}

2. Example

If you want to pass the (=) equal operator into the where() condition, you can omit the operator parameter.

/**
 * Return users
 *
 * @return void
 */
public function index()
{
    $users = User::select('*')
        ->where('is_active', 1)
        ->where('is_delete', 0)
        ->get();
   
    dd($users);
}

3. Example

An array of conditions are used in the where condition.

/**
 * Return users
 *
 * @return void
 */
public function index()
{
    $users = User::select('*')
        ->where([
            ['is_active', '=', 1],
            ['age', '>', 18]
        ])
        ->get();
   
    dd($users);
}

I hope you will like the content and it will help you to learn Laravel Multiple Where Condition Example
If you like this content, do share.


Recommended Posts

View All

Laravel Eloquent Query - Laravel Group By with Order By Desc


You've come to the right place if you're looking for an example of a laravel eloquent group by order by desc.

Laravel whereMonth and whereYear Example


laravel whereMonth and whereYear example, whereMonth, whereYear, where condition in laravel 8, date function in laravel 8, whereMonth and whereYear in...

Laravel 9 Capture Image from Webcam and Store in Database


Learn how to capture images from webcam using Laravel 9 and store them in a database. Step-by-step guide with code examples and best practices.

Laravel 9 Generate Sitemap XML File Tutorial


Learn how to generate a sitemap.xml file in Laravel 9 with this step-by-step tutorial. Improve your website's SEO and enhance user experience.

Laravel 9 Upload image using Summernote editor


A basic Javascript package called Summernote Editor can be used to create online WYSIWYG web editors.