What is Eloquent ORM and why is it needed?
Eloquent ORM (Object-Relational Mapping) is a built-in library for working with databases in the PHP framework Laravel. It provides an elegant, intuitive way to interact with relational databases (MySQL, PostgreSQL, SQLite, SQL Server) using the Active Record pattern.
Why is Eloquent needed?
- SQL Abstraction: Instead of writing raw SQL queries, you work with PHP objects. This makes the code cleaner, more readable, and safer.
- Active Record: Each Eloquent model represents a table in the database. A model instance is a row in that table. You can read, modify, and save data simply by working with object properties.
- Protection against SQL injection: Eloquent uses PDO (PHP Data Objects) with prepared statements, which automatically escapes user input.
- Relationships: Easily define and use relationships between tables (one-to-one, one-to-many, many-to-many, etc.).
- Collections: Query results are returned as collections — powerful objects that provide many useful methods for data processing (map, filter, reduce, pluck, etc.).
- Mutators and Accessors: Allow you to transform data when saving (mutators) or when reading (accessors) from the database.
Eloquent is not just a library; it is a fundamental part of the Laravel ecosystem. Understanding Eloquent is necessary for effective development on this framework.
Installation
Eloquent ORM is installed automatically together with the Laravel framework. If you are creating a new Laravel project, Eloquent is already ready to use.
Installation via Composer (if you are using Laravel):
composer create-project laravel/laravel example-appcd example-appInstalling Eloquent without Laravel (Standalone):
You can use Eloquent outside of Laravel, for example, in your own project. To do this, you will need to install the illuminate/database package and configure the database connection.
composer require illuminate/databasecomposer require illuminate/events // For working with model events (optional)Example of connection setup (standalone):
<?phprequire 'vendor/autoload.php';
use Illuminate\\Database\\Capsule\\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'your_database', 'username' => 'your_username', 'password' => 'your_password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '',]);
// Setting up the global Eloquent instance$capsule->setAsGlobal();
// Booting Eloquent$capsule->bootEloquent();Quick Start with Code Example
Suppose we have a users table in the database. Let's create an Eloquent model to work with it.
Step 1: Creating the Model
In Laravel, models are usually stored in app/Models. Let's create a file User.php:
<?php
namespace App\\Models;
use Illuminate\\Database\\Eloquent\\Model;
class User extends Model{ // Eloquent will automatically link this model to the 'users' table // The table name is determined by the class name in plural (snake_case) // If the table is named differently, specify the $table property // protected $table = 'my_users';
// Specify which fields can be mass-assigned (for protection against mass-assignment) protected $fillable = ['name', 'email', 'password'];}\