$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[February 12, 2025] Eloquent ORM

Eloquent MySQL vs PostgreSQL: Differences

Eloquent MySQL vs PostgreSQL: Differences

────────────────────────────────────────────────────────

Eloquent MySQL vs PostgreSQL: Differences

Tip: JSON Syntax Differs

MySQL: where('data->key', 'value') PostgreSQL: where('data->>key', 'value')

Gotcha: Case Sensitivity

MySQL: case-insensitive by default. PostgreSQL: case-sensitive.

Tip: Boolean Handling

MySQL: stores as TINYINT(0/1). PostgreSQL: native BOOLEAN type.

Gotcha: ILIKE for Case-Insensitive Search

PostgreSQL: where('name', 'ilike', '%john%') MySQL: where('name', 'like', '%john%') (already case-insensitive)

Tip: Array Columns

PostgreSQL has native array columns. MySQL doesn't.

Gotcha: RETURNING Clause

PostgreSQL supports RETURNING on insert. MySQL doesn't. Laravel handles this transparently.

Tip: Use cursor() for Memory-Neutral Iteration

When exporting 100K rows, get() loads everything into memory. cursor() uses yield and keeps memory flat regardless of row count. Perfect for artisan commands.

Tip: whereHas() vs load() — Two Different Things

whereHas() filters the parent query by relationship existence. load() eager-loads relationships AFTER the query. Mixing them up is a common source of logic bugs.

Gotcha: withCount() Adds a Subquery

withCount('comments') runs a correlated subquery on every row. On large tables, this can be slower than a separate query. Profile before relying on it.

Senior Insight

The differences between MySQL and PostgreSQL with Eloquent run deeper than most developers realize. MySQL's GROUP BY is more lenient, PostgreSQL requires all non-aggregated columns in the GROUP BY clause. MySQL's full-text search uses a different syntax than PostgreSQL's tsvector. I've migrated projects from MySQL to PostgreSQL and every migration uncovered Eloquent queries that worked on one but not the other. Always test on both if you support both.

Source: Laravel Docs (https://laravel.com/docs/eloquent), Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/eloquent)

────────────────────────────────────────────────────────
<-- back to posts