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;
- Raw Expressions
- Ordering, Grouping, etc.
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
$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 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() 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.
This is not a query builder. It is used to retrieve a single record and does so using the primary key.
$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.
The following are some of the differences between
findalways uses the primary key by default to filter for records while
whereallows you to use any valid database field
findalways returns a single row while
getmethod which is not limited to returning a single result only
findreturns an object instance of the model while
wherewhich uses the
getmethod returns a collection
nullif no row has been returned while
wherewhich uses the
getmethod always returns a collection which can be empty when no results have been returned from the database.
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
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:
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
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