dev-resources.site
for different kinds of informations.
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: