What is Eloquent ORM in Laravel ?
When building web applications, database interaction is one of the most critical components. Laravel, one of the most popular PHP frameworks, makes this task incredibly easy with its built-in Eloquent ORM (Object-Relational Mapping). Eloquent allows developers to interact with the database in a smooth, elegant and object-oriented way – no raw SQL needed!
What is Eloquent ORM ?
Eloquent is Laravel ‘s implementation of the Active Record pattern. It represents each table in your database as a Model class, and each row in the table as an instance of the model.
This means you can interact with your database like you’re working with regular PHP objects.
$user = User::find(1);
echo $user->name;
No SQL query written, and yet reading from your database.
Getting Started with Eloquent
You can create a model with:
php artisan make:model User
Then easily perform CRUD operations:
User::create(['name' => 'John']);
$user = User::find(1);
$user->update(['name' => 'Jane']);
$user->delete();
Relationships
Eloquent supports relationships like:
One to Many:
public function posts() {
return $this->hasMany(Post::class);
}
Belongs To:
public function user() {
return $this->belongsTo(User::class);
}
Why Use It ?
Eloquent is:
Easy to use
Feature-rich
Great for clean, readable code.