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 9 Scout Full Text Search with Algolia Tutorial


A full-text search feature in a web application is incredibly useful for users to navigate through content-rich web and mobile applications, as demons...

Laravel where clause with date_format() example


laravel db raw where, laravel where date_format, laravel date_format mysql where, laravel date_format example, laravel where with month

How To Use Chart JS In Laravel 


laravel chart js, laravel chartjs ajax example, how to create chart in laravel, how to use chart js in laravel, chartjs laravel example, laravel chart...

Laravel 9 File Manager Tutorial Example


Learn how to implement a robust file manager in Laravel 9 with our step-by-step tutorial. This example will help you manage and organize files efficie...

Laravel Where Clause with MySQL Function Example


laravel eloquent where year,laravel eloquent mysql functions,laravel eloquent where clause mysql,mysql function mysql laravel