What Is an ORM (Object-Relational Mapping)?

An ORM lets you interact with a relational database using your programming language's objects and methods instead of writing raw SQL directly.

Published October 9, 2026

An ORM maps database tables to classes in your language so you can query and save data using objects instead of raw SQL. Laravel Eloquent, Hibernate and SQLAlchemy are examples.

The concept

Each table becomes a model, each row an object and each column a property. The ORM generates SQL for you and handles relationships.

Convenience comes with costs: hidden queries (N+1), less control over performance, and a learning curve.

Object in, SQL out

User::where('active', 1)->orderBy('name')->get()
SELECT * FROM users WHERE active = 1 ORDER BY name

Practical examples

Eloquent CRUD

$u = User::create(['name' => 'Ada']);
$u->update(['name' => 'Ada B']);
$u->delete();

Avoid N+1 with eager loading

$posts = Post::with('author')->get();

Drop to raw SQL when needed

DB::select('SELECT status, COUNT(*) c FROM orders GROUP BY status');

Common mistakes

  • N+1 queries from lazy loading in loops.
  • Loading whole tables into memory.
  • Not looking at the generated SQL.

Key takeaways

  • ORM maps rows to objects.
  • Watch for N+1.
  • Use raw SQL for complex reports.

FAQ

ORM vs query builder?

A query builder composes SQL fluently; an ORM adds objects and relations.

Does an ORM prevent SQL injection?

It uses bound parameters, but raw queries still need care.

More SQL articles