NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior1 view

What is Eloquent? What are its pros and cons, and when should you use it versus avoid it?

Practice

Asked at

AI answer

Eloquent is Laravel's ORM: it represents tables as models and lets you run queries through a PHP API, for example User::where('active', true)->get().

Pros: convenient CRUD, relationship, and migration handling; expressive code; integration with Laravel; support for scopes, casts, and model events.

Cons: an ORM can hide the cost of queries; it's easy to get an N+1 problem when loading relationships; for complex reports and bulk operations, the model API is sometimes less transparent and efficient than SQL or the Query Builder. Behavior also depends on conventions and the model lifecycle.

Eloquent is well suited to ordinary business logic and queries against entities. For complex analytics, fine-grained optimization, bulk updates, or SQL-specific features, consider the Query Builder or explicit SQL. In any case, it is useful to check the actual queries and use eager loading, such as with('orders'), to avoid N+1 problems.