I did some performance test between Laravel query builder and eloquent. Query builder was much faster with various of sql statement (select-update-delete-insert).
So my question is: Why someone uses Laravel Eloquent against plain query builder?
I did some performance test between Laravel query builder and eloquent. Query builder was much faster with various of sql statement (select-update-delete-insert).
So my question is: Why someone uses Laravel Eloquent against plain query builder?
Yes, In some case you are right. When we've more data and almost in every site, data is not small really. Then it is better to use DB Query than the Eloquent Query.
In a performance issue of Eloquent VS DB I've heard that,
So Why then Eloquent ? Is't any necessary of that ?
Answer is - Eloquent is also necessary. Cause-
So when we use Eloquent and When DB facades:
So, finally it is cleared that - when we'll use Database Query and When we'll use Eloquent Query.
Edit - Real Life Example
5,000 teachers and 10,000 students and some notices and files
. Then it is better to do this with simple Laravel Eloquent which is very much standard and readable.1,000,0000 (1 crore) posts and many more things
. I will must choose the conventional DB facades there. It is more faster for searching the posts from so much records.Now it's your choice. What you want to make...
When it comes to performance and the application grows, for the sake of comparison, take a loot at the following tables:
Comparison of select operation average response time between Eloquent ORM and Raw SQL
Eloquent ORM average response time
Raw SQL average response time
Article Reference
It is just my opinion, not a comprehensive answer. I use whatever is more convenient in a given situation.
If I come across a package or code written either in eloquent or query builder, I use whatever is being used.
I found query builder to be more intuitive if I create something from scratch so I use it more often.
When it comes to Laravel, it seems, the ease and speed of developing an app is more important then performance. I really like that they make everything very easy even for someone with little prior knowledge of php/mysql. In some cases eloquent is easier than query builder. In others vice versa. I think having many ways of doing something is what makes Laravel so easy and newbie friendly.
Eloquent is Laravel's implementation of Active Record pattern and it comes with all its strengths and weaknesses. It is a good solution to use when you process a single entity in a CRUD manner - that is, read from database or create a new entity and then save it or delete. You will benefit a lot from Eloquent's features such as dirty checking (to send SQL UPDATE only for the fields which have been changed), model events (e.g. to send administrative alert or update statistics counter when someone has created a new account), traits (timestamps, soft deletes, custom traits) eager/lazy loading etc.
But, as you already know, it comes with some performance price. When you process a single or just a few records, there is nothing to worry about. But for cases when you read lots of records (e.g. for datagrids, for reports, for batch processing etc.) the plain DB is better approach.
For our application we are doing exactly that - using Laravel's Eloquent in web forms to process a single record and using DB (with SQL views) to retrieve data for grids, export etc.
Why Laravel Eloquent:
OOP
manner.query builder
.table schema
. i.e. Even you change your table name, not need to touch a singlequery
(there may have 1000query
) to make it work. Just change the table name inmodel
.JOIN, LEFT JOIN, RIGHT JOIN
etc.) needed in query anymore to get data of related tables.I like using query builder when building complex query from database because it seems easy to use. For working with a single table, I like eloquent.