Introduction to Eloquent ORM
Eloquent ORM is Laravelās built-in Object Relational Mapping system that allows developers to interact with databases using PHP syntax instead of writing complex SQL queries.
Eloquent makes database operations simple, readable, and efficient by representing database tables as models and records as objects.
It is one of the most powerful features of the Laravel framework and is widely used for modern web application development.
Objectives
By the end of this training, you will be able to:
- Understand the concept of ORM
- Learn how Eloquent ORM works in Laravel
- Create and manage models
- Perform database operations using Eloquent
- Work with relationships between tables
- Use migrations with Eloquent models
- Build secure and scalable applications
What is ORM
ORM stands for Object Relational Mapping.
It is a programming technique that connects database tables with object-oriented programming languages.
Instead of writing SQL queries manually, developers can use PHP classes and methods to interact with the database.
Example:
Traditional SQL Query:
SELECT * FROM users;
Eloquent ORM Query:
$users = User::all();
Features of Eloquent ORM
Simple Database Interaction
Eloquent allows developers to insert, update, delete, and retrieve data using clean syntax.
Active Record Pattern
Each database table is connected to a model class.
Built-in Relationships
Eloquent supports one-to-one, one-to-many, many-to-many, and polymorphic relationships.
Query Builder Integration
Eloquent includes Laravelās powerful query builder for advanced database queries.
Secure and Efficient
It helps protect applications from SQL injection attacks through parameter binding.
Creating an Eloquent Model
Models represent database tables in Laravel.
Command to create a model:
php artisan make:model User
Example Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'users';
}
Database Configuration
Database settings are stored in the .env file.
Example:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=training_db
DB_USERNAME=root
DB_PASSWORD=
Retrieving Data with Eloquent
Get All Records
$users = User::all();
Find Record by ID
$user = User::find(1);
Retrieve First Record
$user = User::first();
Using Conditions
$users = User::where('status', 'active')->get();
Inserting Data
$user = new User();
$user->name = 'Ali';
$user->email = 'ali@example.com';
$user->save();
Updating Data
$user = User::find(1);
$user->name = 'Ahmed';
$user->save();
Deleting Data
$user = User::find(1);
$user->delete();
Mass Assignment
Eloquent allows mass assignment for inserting multiple fields.
class User extends Model
{
protected $fillable = ['name', 'email'];
}
Example:
User::create([
'name' => 'Ali',
'email' => 'ali@example.com'
]);
Eloquent Relationships
One-to-One Relationship
public function profile()
{
return $this->hasOne(Profile::class);
}
One-to-Many Relationship
public function posts()
{
return $this->hasMany(Post::class);
}
Many-to-Many Relationship
public function roles()
{
return $this->belongsToMany(Role::class);
}
Using Migrations with Eloquent
Migration command:
php artisan make:migration create_users_table
Example Migration:
Schema::create('users', function ($table) {
$table->id();
$table->string('name');
$table->string('email');
$table->timestamps();
});
Run migration:
php artisan migrate
Eloquent Query Builder
Ordering Data
$users = User::orderBy('name', 'ASC')->get();
Limiting Results
$users = User::take(5)->get();
Counting Records
$count = User::count();
Advantages of Eloquent ORM
- Easy to learn and use
- Reduces SQL complexity
- Clean and readable code
- Faster application development
- Secure database interaction
- Supports powerful relationships
- Integrated with Laravel framework
Best Practices
- Use meaningful model names
- Protect fields using fillable property
- Use migrations for database management
- Optimize queries to improve performance
- Use relationships instead of manual joins where possible
Real World Applications
Eloquent ORM is commonly used in:
- E-commerce applications
- Content management systems
- Learning management systems
- CRM software
- Inventory systems
- Social media platforms
Career Opportunities
Learning Eloquent ORM can help you become:
- Laravel Developer
- Backend Developer
- Full Stack Web Developer
- PHP Application Developer
- API Developer
Final Presentation
In your final presentation, explain:
- What Eloquent ORM is
- Benefits of ORM in web development
- How Eloquent interacts with databases
- CRUD operations using Eloquent
- Relationships in Eloquent ORM
- Real-world usage of Eloquent in Laravel applications