Laravel 9 Where Condition Example

Websolutionstuff | Oct-12-2022 | Categories : Laravel

In this article, we will see laravel 9 where condition example. Where condition is essential in each and every query in laravel. So, we will learn how to write multiple where conditions in laravel 9, and also we will see multiple columns in single where condition.

Where condition has three arguments, the first argument is the name of the column. The second argument is an operator. The third argument is the value to compare against the column's value.

So, let's see where conditions in laravel 9 and multiple where clauses in laravel 9.

Syntax of the where() condition.

where(Column Name, Operator, Value);

 

Example 1:

In this example, we will retrieve the user's records using the operator. This query retrieves votes column equal to 80 and age is greater than 25.

$users = DB::table('users')
                ->where('votes', '=', 80)
                ->where('age', '>', 25)
                ->get();

if you want to verify that a column is = to a given value, you may pass the value as the second argument to the where method.

$users = DB::table('users')->where('votes', 80)->get();

 

Example 2:

In this example, you may pass the multiple arrays of conditions in the single where condition.

$users = DB::table('users')->where([
    ['status', '=', '1'],
    ['plan', '!=', '1'],
    ['closed', '=', false],
])->get();

 

 

Example 3:

In this example, you can use any operator that is supported by your database system.

$users = DB::table('users')
                ->where('votes', '>=', 80)
                ->get();
 
$users = DB::table('users')
                ->where('votes', '<>', 80)
                ->get();
 
$users = DB::table('users')
                ->where('name', 'like', 'web%')
                ->get();

 

Example 4:

In this example, we will retrieve records using the model.

$users = User::where('is_active','=', true)->get();

 


You might also like:

Recommended Post
Featured Post
Laravel 8 REST API With Passport Authentication
Laravel 8 REST API With Passpo...

Hello Guys,  Today I will give you example of laravel 8 REST API with passport authentication. Also perform...

Read More

May-31-2021

How to Create Custom Login and Registration in Laravel 10
How to Create Custom Login and...

In the ever-evolving landscape of web development, crafting a tailor-made user authentication system stands as a pivotal...

Read More

Aug-25-2023

Laravel 8 Inner Join Query Example
Laravel 8 Inner Join Query Exa...

In this tutorial we will learn about laravel 8 inner join query example. Also see how to join two tables in laravel 8. I...

Read More

Nov-24-2021

Laravel 8 Multiple Database Connections
Laravel 8 Multiple Database Co...

Hello Freinds, In this tutorial we will see laravel 8 multiple database connections. Today I will give you step by st...

Read More

Apr-19-2021