0

Eloquent — Laravel's ActiveRecord ORM implementation name

Beginner5 min read·eng-14-009

Concept

Eloquent — Laravel's ORM (Object-Relational Mapper). The name "Eloquent" refers specifically to the ORM component within the Laravel framework.

What the name means: "Eloquent" = expressive, fluent, well-spoken. Taylor Otwell chose the name to reflect the expressive, readable API the ORM provides.

What Eloquent is technically: An implementation of the ActiveRecord pattern. Each Eloquent model class represents a database table. Each instance represents a row. The model contains BOTH the data AND the logic to persist/query it.

Eloquent vs "the database layer": Eloquent is the ORM. Laravel also has DB:: — the Query Builder — which is lower-level (no models, raw SQL-like API). Eloquent is built ON TOP of the Query Builder.

Key Eloquent concepts:

  • Model: PHP class extending Illuminate\Database\Eloquent\Model. One model = one table (convention).
  • Convention over configuration: User model → users table. created_at / updated_at auto-managed. id as primary key.
  • Relations: hasOne, hasMany, belongsTo, belongsToMany, morphTo, etc. — defined as methods.
  • Collections: Eloquent returns Illuminate\Database\Eloquent\Collection — a powerful wrapper around arrays of models.

When people say "Eloquent": They mean the ORM. "The Eloquent query" = a query using the model class. "Eloquent relation" = a relationship defined via method.

Eloquent vs Doctrine: Laravel uses Eloquent (ActiveRecord). Symfony typically uses Doctrine (DataMapper). They're competing ORM philosophies.

Code Example

php
<?php
// A minimal Eloquent model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Order extends Model  // extends Model → it's an Eloquent model
{
    // Table: 'orders' (convention: plural snake_case of class name)
    // Primary key: 'id' (convention)
    // Auto-timestamps: created_at, updated_at (convention)

    protected $fillable = ['user_id', 'total', 'status'];

    // Relations — methods return Eloquent relation objects
    public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function items(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(OrderItem::class);
    }
}

// ELOQUENT USAGE — expressive ("eloquent") API
$orders = Order::where('status', 'pending')
               ->with('user', 'items')        // eager-load relations
               ->orderBy('created_at', 'desc')
               ->paginate(15);

// Each $order is an Order model instance
foreach ($orders as $order) {
    echo $order->status;           // reading attribute
    echo $order->user->name;       // accessing relation (already loaded)
    echo $order->items->count();   // collection method
}

// Creating and saving
$order = Order::create(['user_id' => 1, 'total' => 99.99, 'status' => 'pending']);
$order->status = 'shipped';
$order->save();

// Fluent API (what "eloquent" means)
User::where('active', true)
    ->whereHas('orders', fn($q) => $q->where('total', '>', 100))
    ->with(['profile', 'roles'])
    ->orderBy('name')
    ->get()
    ->each(fn(User $u) => \Log::info($u->name));

// vs DB::table (Query Builder — not Eloquent)
\DB::table('orders')                      // returns raw stdClass objects, not Order models
    ->where('status', 'pending')
    ->orderBy('created_at')
    ->get();

// Eloquent IS the Query Builder extended
// Order::where() → Eloquent Builder → wraps Query Builder → returns Order models