You are on page 1of 4

Blog Categories  Tips & Tricks Contact

HOME LARAVEL HOW TO USE WHERE CONDITION IN LARAVEL ELOQUENT?


RECENT POSTS

How to Use Where Condition in


Laravel Eloquent?
How to Use Where Condition in Laravel
How to Install Laravel 8
Eloquent? Framework on Windows, macOS
and Linux?
HOW TO LARAVEL

How to Add WooCommerce Cart


AMAN MEHRA MAY 26, 2021 LEAVE A COMMENT
Icon in Menu with Item Count?

Tweet on Twitter Share on Facebook How to Secure Password Using


bcrypt in PHP?

How to Make a DataTable in


ReactJS?

CATEGORIES

How To

Laravel

PHP

ReactJS

Tips & Tricks

In this tutorial, we will learn how you can use where condition in laravel WooCommerce
eloquent. It is very easy and short as compared to SQL raw query. We will also
WordPress
discuss multiple where conditions. Let’s start with it.

What is Laravel Eloquent? MOST VIEWED POSTS

WooCommerce Remove
Before proceeding further, you should familiar with what is eloquent?
Update Cart Button and
Make it Automatically
Eloquent is an ORM (Object Relation Mapper) and it helps to interact with the Update.
October 5, 2020
database and get the records from the database table according to your query.
Eloquent has the ability to insert new records, delete, update records with the How to Change Price of
Speci c Product and
built-in very easy functions.
Quantity in Cart?
October 14, 2020
A big advantage of eloquent is that we can easily manage the relationship
How to Redirect to
between two tables or more and in a very short query. We don’t need to write the
Checkout After Add to
long query for the two tables’ joins the same as in the SQL query. Cart?
October 16, 2020

Let’s start with the Where condition to using in the query.


How can I Prevent SQL
Injection in PHP?

Where Condition in Laravel Eloquent October 7, 2020

Eloquent where condition use to get data from database based the speci c Upload Multiple Featured
Images in a Post OR Page
conditions. We already knew about SQL query with where condition and it is January 4, 2021
very long. So eloquent provide you short, fast, easily retrieves data from the
database table. We can also add multiple where condition and use them in the
query builder.
FOLLOW US
SQL Query Syntax: Stay updated via social channels

Select * FROM table_name WHERE column_name = 'value'

Eloquent Query Syntax

where('COLUMN_NAME', 'OPERATOR', 'VALUE')

See the difference above, eloquent is very easy to use and learn and it is fast
also.

Let’s use the where condition in laravel for example.


Method 1: With Operator

In this method, we will use the operator inside the where condition. See the
example below.

1 public function index() {


2 $projects = Project::where("status", "=", 1)->get()
3
4 dd($projects);
5 }

Method 2: Without Operator

1 public function index() {


2 $projects = Project::where("status", 1)->get();
3
4 dd($projects);
5 }

In both above examples will return the same result, only the difference between
is an operator. In the rst example, we had used OPERATOR (=) to check the
status column and in the second example we haven’t used OPERATOR because
it is the magic of eloquent and it understands automatically for the operator. But
it will only work for Equal Operator (=). For other operators, you have to de ne.

Note: Clari ng again, without operator query only work for EQUAL OPERATOR
(=).

Multiple Where Condition in Laravel Eloquent

If you have multiple conditions to check in the database table then you have to
write multiple where condition in the query. Let’s see the example.

1 public function index() {


2 $projects = Project::where(['status' => '1', 'not_co
3
4 dd($projects);
5 }

This will return the result with an exact match of all conditions with the EQUAL
OPERATOR.

OR

But what if you want to add a comparison operator and want to compare the
value, So let’s see the following example for that.

1 public function index() {


2 $projects = Project::where([['status', '=', '1'], [
3
4 dd($projects);
5 }

OR

You can also do the following.

1 public function index() {


2 $projects = Project::where('status', '=', '1')
3 ->('not_completed', '!=', '1')
4 ->('user_id', '>=', '24')
5 ->get();
6
7 dd($projects);
8 }

More Laravel Articles


How to Install Laravel Framework?
Create Model, Migration, Controller and Resource in one Command

If you have any questions or you stuck at any point please ask me in the
comment section. I’ll happy to help you with that.

ELOQUENT ELOQUENT WHERE CONDITION LARAVEL LARAVEL 8


LARAVEL ELOQUENT MULTIPLE WHERE CONDITION QUERY BUILDER
WHERE CONDITION IN LARAVEL

Tweet on Twitter Share on Facebook

YOU MAY ALSO LIKE

How to Install Laravel 8 Framework How to Add WooCommerce Cart Icon


on Windows, macOS and Linux? in Menu with Item Count?

How to Secure Password Using How to Make a DataTable in


bcrypt in PHP? ReactJS?

How to Inline Style in ReactJS? How to Install WordPress Theme


(2021 Edition)?

ABOUT THE AUTHOR: AMAN MEHRA


Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+
years of experience. I love coding and help to people with this blog.

LEAVE A REPLY

Your email address will not be published. Required elds are marked *

Comment

Name * Email * Website

Save my name, email, and website in this browser for the next time I comment.

POST COMMENT
ABOUT QUICK LINKS RECENT POSTS JOIN OUR NEWSLETTER

Your Blog Coach is the best site Blog How to Use Where Condition in Name
for nding the solution to any Laravel Eloquent?
WordPress
issue related to coding and learn
How to Install Laravel 8
more cool stuff and tricks. WooCommerce Email
Framework on Windows, macOS
Contact and Linux?

How to Add WooCommerce Cart


SUBSCRIBE
Icon in Menu with Item Count?

© 2020 Your Blog Coach Privacy Policy Terms and Conditions Sitemap

You might also like