Beginner LevelLaravel
What is Eloquent ORM and how does the Active Record pattern work?
❓ Interview Prompt: Explain Eloquent ORM, ActiveRecord pattern, database table mapping conventions, and basic CRUD operations.
Eloquent ORM provides an ActiveRecord implementation where database tables map directly to PHP Model classes.
Conventions:
•
Table name is the plural form of the Model name ('User' model $
ightarrow$ 'users' table).
•
Primary key defaults to auto-incrementing 'id'.
•
Automatically manages 'created_at' and 'updated_at' timestamps.
CRUD Operations:
•
Create: 'User::create(["name" => "Alice"])'
•
Read: 'User::where("active", true)->get()'
•
Update: '$user->update(["status" => "active"])'
•
Delete: '$user->delete()'
Code Implementation Example
php
<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Article extends Model
{
protected $fillable = ['title', 'slug', 'body', 'published_at'];
}
// Usage in Controller:
$publishedArticles = Article::whereNotNull('published_at')->get();💡Eloquent Model definition with fillable mass-assignment protection.
🎯 Interview Key Takeaway
Eloquent maps database tables to PHP models, providing intuitive ActiveRecord methods for database operations.
#Eloquent#ORM#ActiveRecord#Beginner
Back to all Laravel Questions→