1

I have multiple migrations (say around 10) and the corresponding models for the tables. The problem I am facing is that all the migrations/tables have multiple primary keys but the model has just a string variable to define primary key ($primaryKey), hence, when I save or update a table row using where clause it would just take one primary key and miss the other and hence end up changing multiple rows instead of one. So, basically I switched to DB Queries and it worked well.

So, my question to you is that is there any performance gain with model? Or it is just a designing paradigm? Is there any option to do the same thing (Have multiple primary key) within a model? I know by overriding the Eloquent methods we can do this but is there any other good option?

1 Answer 1

4

Using an ORM like Eloquent is a convenience, not a requirement. From what I've heard there's actually often a slight performance decrease when using an ORM because there's the additional overhead of translating that query into the relevant SQL.

Often, using an ORM makes for queries that are much easier to understand, and for that reason alone it's worth using. However, for more complex queries, an ORM is likely to get in the way, and so you should consider using another method to query the database. You don't have to use just an ORM - you can mix and match the different methods as you see fit.

Laravel has the Query Builder as an alternative to writing raw SQL or using the ORM, and that may be a better option for you here. I would avoid writing raw SQL if you can because both Eloquent and the Query Builder will handle escaping the parameters for you, to help avoid SQL injection vulnerabilities.

My choice would be to use the ORM where possible, and fall back to the Query Builder when the ORM gets in the way.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, it is interesting to read that it might lead to performance decrease. So, as per your answer I understand that in such cases we should just simply create a model without extending Eloquent and then write the functions using Query Builder or any other method, right?
Yes, I would agree with that - just add a method to the model to return the data you need. The main reason to use an ORM is that it's more readable for the developer, but for more complex queries you do need a fallback. I still highly recommend using an ORM where possible because it's generally a lot quicker and easier to work with, and the overhead isn't generally that big a deal. If you're making a query often, you should probably cache it anyway.
Cool, Thanks for your input..Ya, I like the way ORM simplifies it in most of the cases so would try to stick to it wherever possible but for this case I believe its better to work on Query Builder. Thanks again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.