Difference between find and where clause in Laravel

Joy Joel
Joy Joel
Jan 11 · 4 min read

Laravel’s database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems.

There are several database query builder, like;

  • Aggregates
  • Chunking
  • Selects
  • Joins
  • Unions
  • Raw Expressions
  • Where
  • Ordering, Grouping, etc.

Where Clause

the where method is used on a query builder instance to add where clauses to the query. Let’s now look at how to retrieve a record using the where clause:

$c->where('id','=',1)->get();

$c->where('id','=',1)->get() calls the where method on our instance variable. We pass in the first argument id which is the database field where we want to apply the filter. = is the comparison operator for our condition. 1 is the filter value for our condition. We then call the get method which reprieves the record. The SQL statement equivalent of this query is SELECT * FROM categories WHERE id = 1.

Note: I am using an existing laravel project and php tinker on my terminal to practice. On your terminal, type php artisan tinker

Result of the above code

Result returned is a collection and not an object of our model. A collection is simply an array that contains objects of all database results returned.

You can rewrite the above query in shorthand syntax

$c->whereId(1)->get();

$c->whereId(1)->get() using camel casing to construct our where clause and laravel treats a Capital letter which it encounters after the where, as a database field and constructs the necessary query.

Find Method

This is not a query builder. It is used to retrieve a single record and does so using the primary key.

$c->find(1)

$c->find(1) calls the find method on our instance variable $c which returns a single record. find uses the primary key id which filters for a record with the id value of 1. Its SQL statement equivalent is SELECT * FROM categories WHERE id = 1 LIMIT 1. The results of find are always limited to record and the result returned is an object.

Result for the above query

The following are some of the differences between find and where methods;

  • find always uses the primary key by default to filter for records while where allows you to use any valid database field
  • find always returns a single row while where calls the get method which is not limited to returning a single result only
  • find returns an object instance of the model while where which uses the get method returns a collection
  • find returns null if no row has been returned while where which uses the get method always returns a collection which can be empty when no results have been returned from the database.
attempt to find a record with an id 3, which doesn’t exist. Returns ‘null’

From the image above, when we call the find method, if the record exists then we can an object which is the instance of the Model and the database fields are available as attributes of the class. If the record does not exist, we get a null value.

where clause returns an empty collection

On the other hand, we can limit the results of the where clause to a single row only by calling the first method on the instance variable like this:

$c->whereId(1)->first()

$c->whereId(1)->first() the first method behaves similar to the find method. Laravel will search for the records that match the filter criteria but only returns the first record that matches the search criteria.

I had to write because i have been finding it difficult to understand query builder and their uses. I also wouldn’t tell the difference between find and where queries and i know someone might be going through the same challenge

Don’t forget to leave claps for me. Thank you and Happy reading

Joy Joel

Written by

Joy Joel

Christian|Nigerian|Techie Newbie|Netpreneur|Music Lover|A Consistent Learner

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade