13 - Eloquent vs Query Builder: whereHas vs join() vs Raw SQL
We found 8 results for "eloquent vs query builder".
When selecting data form the database, sometimes you need to make some extra filtering with results - with some if-else statements and similar. With Laravel - you can achieve that with Accessor fields or just looping through results in PHP. But there is a more effective way - to move the filters to the database query itself.
I know the title sounds unclear, so let me start with an example. What would be your Eloquent query if you have birth_date field in DB an want to show how many of your users are adult 18+ years, and how many are still children? Let's take a look.
To add a record to the database, you may use one of two methods. What is the difference?
I've been asked this question quite a lot, so decided to discuss the pros and cons of each approach.
A quick video showing how to protect from SQL injection in raw queries of Laravel Query Builder.
If you have a query with the `belongsTo` relationship and want to order the records by the column from that related table, the orderBy wouldn't work.
In one of the recent Laravel versions, they released an improvement to use the `touch()` method on Query Builder, and not only on Eloquent Model.