Eloquent ORM is Laravel’s built-in Object Relational Mapping (ORM) system used to interact with databases using PHP objects instead of raw SQL queries.
It allows developers to work with database records in a clean, readable, and efficient way.
What is Eloquent ORM?
Eloquent ORM is an abstraction layer that represents database tables as PHP classes and rows as objects.
Each model in Laravel corresponds to a database table and provides methods for performing database operations.
Why Use Eloquent?
Eloquent simplifies database queries, reduces boilerplate code, and improves readability.
It also provides built-in support for relationships, query scopes, and data manipulation.
Creating an Eloquent Model
Models are created using Laravel’s Artisan command.
php artisan make:model PostBasic Model Structure
A model represents a database table and is stored inside the app/Models directory.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
}
?>Retrieving All Records
Eloquent provides simple methods to fetch data from the database.
<?php
use App\Models\Post;
$posts = Post::all();
?>Finding Records by ID
You can retrieve a single record using its primary key.
<?php
$post = Post::find(1);
?>Conditional Queries
Eloquent allows filtering data using expressive query methods.
<?php
$posts = Post::where('status', 'published')->get();
?>Creating Records
You can insert new records into the database using the create method.
<?php
Post::create([
'title' => 'First Post',
'content' => 'This is a post content'
]);
?>Mass Assignment Protection
Laravel protects against mass assignment vulnerabilities using the $fillable property.
<?php
class Post extends Model
{
protected $fillable = ['title', 'content'];
}
?>Updating Records
Eloquent makes updating records straightforward using object methods.
<?php
$post = Post::find(1);
$post->title = 'Updated Title';
$post->save();
?>Deleting Records
Records can be deleted using the delete method.
<?php
$post = Post::find(1);
$post->delete();
?>Eloquent Relationships
Eloquent supports relationships such as one-to-one, one-to-many, and many-to-many.
One-to-Many Example
<?php
class User extends Model
{
public function posts() {
return $this->hasMany(Post::class);
}
}
?>Inverse Relationship
<?php
class Post extends Model
{
public function user() {
return $this->belongsTo(User::class);
}
}
?>Eager Loading
Eager loading helps reduce database queries by loading related models in advance.
<?php
$users = User::with('posts')->get();
?>Query Scopes
Query scopes allow reusable query logic inside models.
<?php
public function scopeActive($query) {
return $query->where('status', 'active');
}
?>Accessing Scopes
<?php
$users = User::active()->get();
?>Timestamps in Eloquent
Eloquent automatically manages created_at and updated_at timestamps.
This helps track when records are created or modified.
Common Beginner Mistakes
Beginners often forget to define fillable properties, leading to mass assignment errors.
Another mistake is overusing raw queries instead of Eloquent methods.
Best Practices
Use Eloquent relationships, scopes, and mass assignment protection properly.
Keep database logic inside models to maintain clean architecture.
Real-World Importance
Eloquent is widely used in real-world Laravel applications to simplify database operations.
It helps developers build scalable and maintainable applications faster.
Summary
Eloquent ORM simplifies database interaction by using PHP models instead of raw SQL queries.
Mastering Eloquent is essential for building modern Laravel applications efficiently.