Overview
Eloquent is the ORM included with Laravel. Each database table has a model class, created with php artisan make:model, that follows naming conventions for table names, primary keys and timestamps. Models query their table fluently (Flight::where('active', 1)->orderBy('name')->get()), and relationships such as hasMany, belongsTo, belongsToMany and polymorphic relations are declared as methods and eager-loaded with with().
Laravel provides first-party support for MariaDB 10.3+, MySQL 5.7+, PostgreSQL 10+, SQLite 3.26+ and SQL Server 2017+, and MongoDB maintains an official package for it. Schema changes are made with Laravel migrations, which are generated by Artisan and written with the database-agnostic Schema facade; migrations can be squashed into a schema dump as a project grows.
Eloquent also offers casts, accessors and mutators, global and local scopes, model events and observers, soft deletes, pruning and strictness settings that can flag lazy loading during development. It is tied to Laravel's Illuminate components; using it outside Laravel is possible but less common.
Pricing and plans
| Plan | Price | What you get |
|---|---|---|
| Eloquent (Laravel) | $0 | Part of the MIT-licensed Laravel framework |
What it does
- Active Record models
- Each model reads and writes its own table, with conventions for names, keys and timestamps.
- Relationships
- One-to-one, one-to-many, many-to-many, has-many-through and polymorphic relations with eager loading.
- Scopes, casts and events
- Global and local query scopes, attribute casting and model events or observers.
- Soft deletes and pruning
- Mark rows deleted instead of removing them, and prune old models on a schedule.
- Migrations
- Artisan-generated migrations using the Schema builder, with rollback and schema squashing.
- Strict mode
- Options to prevent lazy loading and silently discarded attributes during development.
Strengths and limitations
Strengths
- Very quick to build CRUD features with Artisan generators
- Readable, expressive API and excellent Laravel documentation
- Large ecosystem of packages and tutorials
- Five first-party databases plus official MongoDB support
Limitations
- Tied to Laravel; standalone use outside the framework is uncommon
- Active Record mixes persistence into domain models, which can grow large
- Lazy loading can cause N+1 queries unless you eager-load or enable strict mode
- Migrations are written by hand, not generated by diffing models
Who it suits
Good for
- Laravel applications
- Teams that want fast CRUD development in PHP
- SaaS products built on Laravel's ecosystem
Look elsewhere if
- Symfony or framework-agnostic PHP projects (Doctrine fits better)
- Complex domain models that benefit from a data mapper
Alternatives to Eloquent ORM
| Tool | From | Free option | Stages |
|---|---|---|---|
| Active Record (Rails) Active Record ORM for Ruby on Rails | — | ||
| Dapper .NET micro-ORM | — | ||
| SQLAlchemy Python SQL toolkit and ORM | — | ||
| Entity Framework Core .NET ORM | — | ||
| sqlc SQL-to-code compiler | — | ||
| TypeORM Decorator-based TypeScript ORM | — |
Questions
Which databases does Eloquent support?
MariaDB, MySQL, PostgreSQL, SQLite and SQL Server first-party, and MongoDB through the mongodb/laravel-mongodb package maintained by MongoDB.
Does Eloquent generate migrations from models?
No. Artisan generates migration files, which you write with the Schema builder.
Can I use Eloquent without Laravel?
It is possible through the Illuminate database component, but it is designed and documented for use inside Laravel.
Sources read for this page