where
is a method used in Laravel's Fluent Query Builder. This method is used to filter a database query. It is often used to retrieve records with a specific condition or set of conditions.
$users = DB::table('users')
->where('status', '=', 'active')
->get();
In this example, we retrieve all users with a value of 'active
' in the status column in the users
table.
Sometimes a single condition may not be enough. You can further filter queries using multiple conditions in Laravel
$users = DB::table('users')
->where('status', '=', 'active')
->where('type', '=', 'admin')
->get();
This query returns all users with 'active
' in the status column and 'admin
' in the type column.
where
method can also be used with different comparison operators
$users = DB::table('users')
->where('age', '>', 18)
->get();
This query will return all users with age
column greater than 18.
Sometimes, you may need to write SQL queries directly instead of using Laravel's Fluent Query Builder. In this case, you can use the whereRaw method
$users = DB::table('users')
->whereRaw('age > ? and status = ?', [18, 'active'])
->get();
In this example, we fetch all users who are greater than 18 in the age
column and 'active' in the status column.