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.