Skip to content

Eloquent ORM

Eloquent is Laravel's built-in ORM (Object-Relational Mapper) that provides an elegant way to interact with databases using PHP objects instead of raw SQL.

Why It Matters

Eloquent makes database operations intuitive and readable, reducing bugs while speeding development with relationships, scopes, and mutators built in.

When to Use This

  • Querying database records with chained conditions
  • Defining model relationships (users have many posts)
  • Automatic timestamp management
  • Soft deletes for non-destructive data removal

Examples

  • User::where('active', true)->with('posts')->get()
  • $user->posts()->create(['title' => 'My Post'])
  • Product::findOrFail($id) with automatic 404

Related Terms