Logo

dev-resources.site

for different kinds of informations.

Laravel Eloquent ORM in Bangla Part-2 (Eloquent Model Conventions)

Published at
1/5/2025
Categories
Author
Sontus Chandra Anik
Categories
1 categories in total
open
Laravel Eloquent ORM in Bangla Part-2 (Eloquent Model Conventions)

Laravel Eloquent Model Conventions āĻšāĻ˛ā§‹ āĻāĻ•āĻŸāĻŋ āĻ¸ā§‡āĻŸ āĻ°ā§āĻ˛ āĻŦāĻž āĻĄāĻŋāĻĢāĻ˛ā§āĻŸ āĻ¨āĻŋā§ŸāĻŽ āĻ¯āĻž āĻŽāĻĄā§‡āĻ˛ āĻāĻŦāĻ‚ āĻĄāĻžāĻŸāĻžāĻŦā§‡āĻ¸ āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻŽāĻ§ā§āĻ¯ā§‡ āĻ¸āĻ‚āĻ¯ā§‹āĻ— āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°ā§‡āĨ¤ āĻāĻ‡ āĻ•āĻ¨āĻ­ā§‡āĻ¨āĻļāĻ¨āĻ—ā§āĻ˛ā§‹ āĻŽā§‡āĻ¨ā§‡ āĻšāĻ˛āĻ˛ā§‡ āĻ•ā§‹āĻĄ āĻ˛ā§‡āĻ–āĻžāĻ° āĻĒāĻ°āĻŋāĻŽāĻžāĻŖ āĻ•āĻŽā§‡ āĻ¯āĻžā§Ÿ āĻāĻŦāĻ‚ āĻĄā§‡āĻ­ā§‡āĻ˛āĻĒāĻŽā§‡āĻ¨ā§āĻŸ āĻ†āĻ°āĻ“ āĻ¸āĻšāĻœ āĻšā§ŸāĨ¤ āĻ¤āĻŦā§‡ āĻ†āĻĒāĻ¨āĻŋ āĻšāĻžāĻ‡āĻ˛ā§‡ āĻāĻ‡ āĻĄāĻŋāĻĢāĻ˛ā§āĻŸ āĻ•āĻ¨āĻ­ā§‡āĻ¨āĻļāĻ¨āĻ—ā§āĻ˛ā§‹ āĻ•āĻžāĻ¸ā§āĻŸāĻŽāĻžāĻ‡āĻœ āĻ•āĻ°ā§‡āĻ“ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°ā§‡āĻ¨āĨ¤

Laravel Eloquent Model Conventions

ā§§. āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻ¨āĻžāĻŽ (Table Name)

āĻĄāĻŋāĻĢāĻ˛ā§āĻŸāĻ­āĻžāĻŦā§‡, Eloquent āĻŽāĻĄā§‡āĻ˛ āĻāĻ•āĻŸāĻŋ āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻ¸āĻžāĻĨā§‡ āĻ¸āĻ‚āĻ¯ā§āĻ•ā§āĻ¤ āĻĨāĻžāĻ•ā§‡ āĻ¯āĻžāĻ° āĻ¨āĻžāĻŽ āĻŽāĻĄā§‡āĻ˛ā§‡āĻ° āĻĒā§āĻ˛ā§āĻ°āĻžāĻ˛ āĻĢāĻ°ā§āĻŽāĨ¤

āĻĄāĻŋāĻĢāĻ˛ā§āĻŸ āĻ¨āĻŋā§ŸāĻŽ:

  • āĻŽāĻĄā§‡āĻ˛: Post
  • āĻŸā§‡āĻŦāĻŋāĻ˛: posts
  • āĻŽāĻĄā§‡āĻ˛: Category
  • āĻŸā§‡āĻŦāĻŋāĻ˛: categories

āĻ•āĻžāĻ¸ā§āĻŸāĻŽ āĻŸā§‡āĻŦāĻŋāĻ˛ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ°:

āĻ†āĻĒāĻ¨āĻŋ $table āĻĒā§āĻ°āĻĒāĻžāĻ°ā§āĻŸāĻŋ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻ•āĻžāĻ¸ā§āĻŸāĻŽ āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻ¨āĻžāĻŽ āĻ¸ā§‡āĻŸ āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°ā§‡āĻ¨āĨ¤

class Post extends Model
{
    protected $table = 'blog_posts';
}

ā§¨. āĻĒā§āĻ°āĻžāĻ‡āĻŽāĻžāĻ°āĻŋ āĻ•ā§€ (Primary Key)

Eloquent āĻĄāĻŋāĻĢāĻ˛ā§āĻŸāĻ­āĻžāĻŦā§‡ āĻ§āĻ°ā§‡ āĻ¨ā§‡ā§Ÿ āĻ¯ā§‡ āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻĒā§āĻ°āĻžāĻ‡āĻŽāĻžāĻ°āĻŋ āĻ•ā§€ āĻšāĻ˛ā§‹ idāĨ¤

āĻ•āĻžāĻ¸ā§āĻŸāĻŽ āĻĒā§āĻ°āĻžāĻ‡āĻŽāĻžāĻ°āĻŋ āĻ•ā§€ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ°:

āĻ†āĻĒāĻ¨āĻŋ $primaryKey āĻĒā§āĻ°ā§‹āĻĒāĻžāĻ°ā§āĻŸāĻŋ āĻĻāĻŋāĻ¯āĻŧā§‡ āĻĒā§āĻ°āĻžāĻ‡āĻŽāĻžāĻ°āĻŋ āĻ•ā§€ āĻ•āĻžāĻ¸ā§āĻŸāĻŽāĻžāĻ‡āĻœ āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°ā§‡āĻ¨āĨ¤

class Post extends Model
{
    protected $primaryKey = 'post_id';
}

āĻ…āĻŸā§‹-āĻ‡āĻ¨āĻ•ā§āĻ°āĻŋāĻŽā§‡āĻ¨ā§āĻŸ āĻāĻŦāĻ‚ āĻ¨āĻ¨-āĻ‡āĻ¨āĻ•ā§āĻ°āĻŋāĻŽā§‡āĻ¨ā§āĻŸ āĻ•ā§€:

āĻ¯āĻĻāĻŋ āĻĒā§āĻ°āĻžāĻ‡āĻŽāĻžāĻ°āĻŋ āĻ•ā§€ āĻ‡āĻ¨āĻ•ā§āĻ°āĻŋāĻŽā§‡āĻ¨ā§āĻŸāĻžāĻ˛ āĻ¨āĻž āĻšāĻ¯āĻŧ, āĻ¤āĻžāĻšāĻ˛ā§‡ $incrementing āĻĒā§āĻ°āĻĒāĻžāĻ°ā§āĻŸāĻŋ false āĻ•āĻ°ā§āĻ¨āĨ¤

class Post extends Model
{
    protected $primaryKey = 'uuid';
    public $incrementing = false;
}

ā§Š. āĻŸāĻžāĻ‡āĻŽāĻ¸ā§āĻŸā§āĻ¯āĻžāĻŽā§āĻĒ āĻ•āĻ˛āĻžāĻŽ (Timestamps)

Laravel āĻ¸ā§āĻŦāĻ¯āĻŧāĻ‚āĻ•ā§āĻ°āĻŋāĻ¯āĻŧāĻ­āĻžāĻŦā§‡ created_at āĻāĻŦāĻ‚ updated_at āĻ•āĻ˛āĻžāĻŽ āĻĒāĻ°āĻŋāĻšāĻžāĻ˛āĻ¨āĻž āĻ•āĻ°ā§‡āĨ¤

āĻŸāĻžāĻ‡āĻŽāĻ¸ā§āĻŸā§āĻ¯āĻžāĻŽā§āĻĒ āĻŦāĻ¨ā§āĻ§ āĻ•āĻ°āĻ¤ā§‡:

class Post extends Model
{
    public $timestamps = false;
}

āĻ•āĻžāĻ¸ā§āĻŸāĻŽ āĻŸāĻžāĻ‡āĻŽāĻ¸ā§āĻŸā§āĻ¯āĻžāĻŽā§āĻĒ āĻ•āĻ˛āĻžāĻŽā§‡āĻ° āĻ¨āĻžāĻŽ:

class Post extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'last_update';
}

ā§Ē. āĻĢāĻŋāĻ˛ā§‡āĻŦāĻ˛ āĻāĻŦāĻ‚ āĻ—āĻžāĻ°ā§āĻĄā§‡āĻĄ āĻĢāĻŋāĻ˛ā§āĻĄāĻ¸ (Mass Assignment)

āĻĄāĻŋāĻĢāĻ˛ā§āĻŸāĻ­āĻžāĻŦā§‡, Laravel mass assignment āĻĒā§āĻ°āĻŸā§‡āĻ•āĻļāĻ¨ āĻĻā§‡āĻ¯āĻŧāĨ¤

$fillable:

$fillable āĻ¨āĻŋāĻ°ā§āĻ§āĻžāĻ°āĻŖ āĻ•āĻ°ā§‡ āĻ•ā§‹āĻ¨ āĻĢāĻŋāĻ˛ā§āĻĄā§‡ mass assignment āĻ…āĻ¨ā§āĻŽāĻ¤āĻŋ āĻĻā§‡āĻ“āĻ¯āĻŧāĻž āĻšāĻŦā§‡āĨ¤

class Post extends Model
{
    protected $fillable = ['title', 'content'];
}

$guarded:

$guarded āĻ¨āĻŋāĻ°ā§āĻ§āĻžāĻ°āĻŖ āĻ•āĻ°ā§‡ āĻ•ā§‹āĻ¨ āĻĢāĻŋāĻ˛ā§āĻĄāĻ—ā§āĻ˛ā§‹āĻ¤ā§‡ mass assignment āĻ¨āĻŋāĻˇāĻŋāĻĻā§āĻ§āĨ¤

class Post extends Model
{
    protected $guarded = ['id'];
}

ā§Ģ. āĻ¸āĻŽā§āĻĒāĻ°ā§āĻ• (Relationships)

Eloquent āĻ¸āĻŽā§āĻĒāĻ°ā§āĻ• āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ°ā§‡āĻ° āĻœāĻ¨ā§āĻ¯ āĻ•āĻŋāĻ›ā§ āĻĄāĻŋāĻĢāĻ˛ā§āĻŸ āĻ•āĻ¨āĻ­ā§‡āĻ¨āĻļāĻ¨ āĻ°āĻ¯āĻŧā§‡āĻ›ā§‡āĨ¤

One-to-One Relationship:

class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

One-to-Many Relationship:

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

Many-to-Many Relationship:

class Post extends Model
{
    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
}

ā§Ŧ. āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻĄāĻŋāĻĢāĻ˛ā§āĻŸ āĻ¨āĻžāĻŽāĻ•āĻ°āĻŖ (Pivot Table)

Many-to-Many āĻ¸āĻŽā§āĻĒāĻ°ā§āĻ•ā§‡āĻ° āĻ•ā§āĻˇā§‡āĻ¤ā§āĻ°ā§‡ Laravel āĻĄāĻŋāĻĢāĻ˛ā§āĻŸāĻ­āĻžāĻŦā§‡ āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻ¨āĻžāĻŽ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°ā§‡ āĻĻā§āĻ‡ āĻŽāĻĄā§‡āĻ˛ā§‡āĻ° āĻ¨āĻžāĻŽā§‡āĻ° āĻ…ā§āĻ¯āĻžāĻ˛āĻĢāĻžāĻŦā§‡āĻŸāĻŋāĻ• āĻ•ā§āĻ°āĻŽāĻžāĻ¨ā§āĻ¸āĻžāĻ°ā§‡āĨ¤

āĻĄāĻŋāĻĢāĻ˛ā§āĻŸ āĻŸā§‡āĻŦāĻŋāĻ˛ āĻ¨āĻžāĻŽ:

post_tag (āĻ¯āĻĻāĻŋ āĻŽāĻĄā§‡āĻ˛ Post āĻāĻŦāĻ‚ Tag āĻšāĻ¯āĻŧ)āĨ¤

āĻ•āĻžāĻ¸ā§āĻŸāĻŽ āĻĒāĻŋāĻ­āĻŸ āĻŸā§‡āĻŦāĻŋāĻ˛ āĻ¨āĻžāĻŽ:

class Post extends Model
{
    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'custom_pivot_table');
    }
}

ā§­. āĻŽāĻĄā§‡āĻ˛ āĻ‡āĻ­ā§‡āĻ¨ā§āĻŸāĻ¸ (Model Events)

Eloquent āĻŽāĻĄā§‡āĻ˛ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āĻ¨ āĻ‡āĻ­ā§‡āĻ¨ā§āĻŸ (āĻ¯ā§‡āĻŽāĻ¨: creating, updating, deleting) āĻŸā§āĻ°āĻŋāĻ—āĻžāĻ° āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°ā§‡āĨ¤

class Post extends Model
{
    protected static function booted()
    {
        static::creating(function ($post) {
            $post->slug = Str::slug($post->title);
        });
    }
}

ā§Ž. āĻ•āĻžāĻ¸ā§āĻŸāĻŽ āĻ•ā§āĻ¯āĻžāĻļāĻŋāĻ‚ āĻ•āĻ¨āĻ­ā§‡āĻ¨āĻļāĻ¨ (Global Scopes)

āĻ•ā§‹āĻ¨ā§‹ āĻŽāĻĄā§‡āĻ˛ā§‡ āĻ—ā§āĻ˛ā§‹āĻŦāĻžāĻ˛ āĻ¸ā§āĻ•ā§‹āĻĒ āĻ¯ā§āĻ•ā§āĻ¤ āĻ•āĻ°āĻž āĻ¯āĻžāĻ¯āĻŧāĨ¤

class Post extends Model
{
    protected static function booted()
    {
        static::addGlobalScope('published', function ($query) {
            $query->where('status', 'published');
        });
    }
}

Featured ones: