Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jredli93's avatar

Eloquent and QueryBuilder advice

Hey all,

I started learning Laravel and I have problem understadning the difference between Eloquent and QueryBuilder, when to use which and what is the best practice.

Atm I need to write a complex query that involves 3 tables and I need to groupBy the result and I can't find a way write it(I wrote the raw mysql query easily but have problems converting it to eloquent).

Hope someone can guide me or send me some useful links(I already googled a lot and couldn't find solutions for my problem).

Thanks!

0 likes
1 reply
tykus's avatar

In some respects they are the same, but Eloquent is ORM that uses the Query Builder, while Query Builder is fluent interface for building queries. If your desired result is a Model and you want to use the behaviours on the Model, then Eloquent is needed, Whereas Query Builder will give you basic stdClass instances with properties, and no Model behaviours.

Broadly, I would say that Eloquent is an object representation of a (collection of) record(s) in a table, a User or _ a Post_ etc. Your result will be an instance of the given Eloquent model and will have the ability to load nested relations, accessors and mutators etc.

Whenever the result you want is more than that, e.g. aggregations, multi-table joins etc. it will more often use Query Builder where you translate a SQL query into its Query Builder representation. You will have a flat data structure, with raw data from the query.

1 like

Please or to participate in this conversation.