Member-only story
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