Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent ORM vs Query builder which one better?

Tags:

sql

laravel

Can anyone tell me ORM or query builder which one will be better ?And it's benefit?

like image 247
KaziBablu Avatar asked Mar 05 '18 07:03

KaziBablu


2 Answers

We use frameworks like Laravel to ease our work. When it comes to concept of frameworks, speed and ease of development is more important than performance. So by using ORM’s it provides powerful methods to handle with database without no need of heavy mysql knowledge. In case of query builder it’s provide methods to build queries in efficient manner.

As my experience there are some things that we can’t easily do with Eloquent. So then we need to use query builder to build direct queries.

So I think it’s not good to compare Eloquent and query builder.

You can find more details here.

But I can give you some tips to select one method.

  • If you are more aware of efficiency rather than ease of development, go for query builder.
  • If you dealing with one entity go for ORM, (Eloquent).
  • If you dealing with multiple entities it’s better to deal with query builder.
  • If you are new to mysql or your application is not very complex, definitely choose ORM.
  • If you need more complex query, I recommend to use query builder.
like image 171
dilusha_dasanayaka Avatar answered Oct 10 '22 20:10

dilusha_dasanayaka


ORM

  • Provides you a nice a API in OOP manners
  • You can play with relationships easily
  • You can also Handle Polymorphic Relationships which is not provided in Query Builder
  • You don't have to worry about joins.
  • You can do more with Less code.
  • You can use mutators and setters which saves you a lot of time
  • In Testing You can mock Objects easily.
  • You can Cast the types of columns.

Hope these are enough

like image 35
Romantic Dev Avatar answered Oct 10 '22 20:10

Romantic Dev