Demystifying Laravel Eloquent ORM Link to heading
Eloquent ORM is one of the crown jewels of the Laravel framework. This Object-Relational Mapping (ORM) system allows developers to interact with their database using an expressive, fluent syntax, making it a cornerstone for anyone serious about Laravel development. In today’s post, we’re going to unravel this marvel, providing code examples and insights along the way.
What is Eloquent ORM? Link to heading
Eloquent ORM is Laravel’s built-in ORM implementation that provides a simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.
Setting Up Eloquent Link to heading
First things first, you need to create a model. Suppose we have a users
table:
php artisan make:model User
This command will create a User.php
file in the app/Models
directory. Here’s a basic example of what it might look like:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory;
protected $fillable = ['name', 'email', 'password'];
}
Querying Data Link to heading
Eloquent makes data querying incredibly intuitive. Here are some common examples:
Retrieving All Records Link to heading
$users = User::all();
Retrieving a Record by Primary Key Link to heading
$user = User::find(1);
Adding Constraints Link to heading
$users = User::where('status', 'active')->get();
Retrieving a Single Record Link to heading
$user = User::where('email', 'john@example.com')->first();
Inserting & Updating Records Link to heading
Inserting a New Record Link to heading
$user = new User;
$user->name = 'John Doe';
$user->email = 'john@example.com';
$user->password = bcrypt('password');
$user->save();
Updating an Existing Record Link to heading
$user = User::find(1);
$user->email = 'john.new@example.com';
$user->save();
Relationships Link to heading
Eloquent also provides a simple way to handle relationships between different models. Here are a few examples:
One-to-One Link to heading
class User extends Model
{
public function phone()
{
return $this->hasOne(Phone::class);
}
}
One-to-Many Link to heading
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
Many-to-Many Link to heading
class User extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
Conclusion Link to heading
Laravel’s Eloquent ORM is a powerful tool that can simplify database interactions significantly. With its expressive syntax and built-in features, it allows developers to write cleaner and more maintainable code.
So, whether you’re building a small project or a massive enterprise application, Eloquent ORM can be your trusty sidekick in the world of database management.
Feel free to explore more about Eloquent ORM in the official Laravel documentation.
Image source: Laravel Official Site