6

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

KaziBablu
  • 493
  • 1
  • 6
  • 16
  • Possible duplicate of [Laravel Eloquent vs query builder - Why use eloquent to decrease performance](https://stackoverflow.com/questions/38391710/laravel-eloquent-vs-query-builder-why-use-eloquent-to-decrease-performance) – Bugfixer Mar 05 '18 at 07:20

3 Answers3

6

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.
dilusha_dasanayaka
  • 1,401
  • 2
  • 17
  • 30
4

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

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
3

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.

Edit - Real Life Example

I'm making an University website. Which may contain maximum 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.

Now I'm making a site like Stackoverflow. Which may contains more than 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.

You can check your query performance using Laravel Debugbar (A popular package to check Eloquent/Database query performance/ execution times)

Now it's your choice. What you want to make...

Hussam Adil
  • 502
  • 7
  • 13