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?
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:
- Executing query in a
- Easy to use than raw query or
- No binding with
table schema. i.e. Even you change your table name, not need to touch a single
query(there may have 1000
query) to make it work. Just change the table name in
- Relationship among tables can be maintain in an elegant way. Just mention the type of relationship, nothing else(
JOIN, LEFT JOIN, RIGHT JOINetc.) needed in query anymore to get data of related tables.
- Queries highly readable while written using Eloquent comparing with Query Builder.
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.
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.
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,
To insert 1000 rows for a simple table Eloquent takes 1.2 seconds and
in that case DB facades take only 800 mili seconds(ms).
So Why then Eloquent ? Is’t any necessary of that ?
Answer is – Eloquent is also necessary. Cause-
To create a better relationship and get the results in view with so much simple syntax, when there needs to join.
Eloquent is also for who have not much knowledge of SQL query.
An MVC framework follow the rules of Code Readability, Code Maintainability and which Eloquent is, you know that. A code comparison below. Obviously, Eloquent is better to read.
// In Eloquent $student = App\Student::find($id); // In DB facade $student = DB::table('student')->where('id', $id)->first();
Most important part is if we want to change other database, then raw query will be a lot much headache to us and in that case Laravel Eloquent will solve all the problems with one hand. It can handle different types Database.
So when we use Eloquent and When DB facades:
- When we’ll work on a simple and small records site with simple CRUD and there records
are not fact, then use Eloquent there.
- When we’ll work on a lot’s of records, it is better to use DB Query than Eloquent.
So, finally it is cleared that – when we’ll use Database Query and When we’ll use Eloquent Query.