Eloquent ORM relationships
One-to-Many Relationship
-
hasMany (Parent -> Children): Indicates that a model can
have many tables in another model.
One-to-One Relationship
-
belongsTo (Child -> Parent): Indicates that a model can
have a single table in another model.
- hasOne (Parent -> Child): Indicates that a model can have only one record in another model.
Pivot Tables
Pivot tables are used in many-to-many relationships to represent the relationship between two models. These tables are usually named in alphabetical order with singular names.
Many-to-Many Relationship
- belongsToMany (Both Sides -> Each Other): Indicates a many-to-many relationship
between two models. This relationship is usually managed via a pivot
table.
Polymorphic Relationship
- morphTo (Child -> Parent Models): Indicates that a model can belong to
multiple different model types.
- morphMany (Parent -> Many Children): Indicates that a model can have many
records in another model through a polymorphic relationship.
- morphOne (Parent -> Single Child): Indicates that a model can have only one record in another model through a polymorphic relationship.
Polymorphic Many-to-Many Relationship
- morphedByMany (Child -> Parents): Indicates a many-to-many polymorphic
relationship where a model can belong to many different types of
models.
Comparison of BelongsTo and hasMany
Example:
// A category can have many subcategories
// Category Model
class Category extends Model
{
public function exams()
{
return $this->hasMany(Exam::class);
}
}
// SubCategory Model
class SubCategory extends Model
{
public function category()
{
return $this->belongsTo(Category::class);
}
}
Comparison of hasOne and BelongsTo
// User Model: A user can have only one phone number.
class User extends Model
{
public function phone()
{
return $this->hasOne(Phone::class);
}
}
// Phone Model
class Phone extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
belongsToMany
Example:
// A student can enroll in many courses, and many students can enroll in a course.
// Student Model
class Student extends Model
{
public function courses()
{
return $this->belongsToMany(Course::class);
}
}
// Course Model
class Course extends Model
{
public function students()
{
return $this->belongsToMany(Student::class);
}
}
Migration:
Schema::create('course_student', function (Blueprint $table) {
$table->id();
$table->foreignId('course_id')->constrained()->onDelete('cascade');
$table->foreignId('student_id')->constrained()->onDelete('cascade');
$table->timestamps();
});
morphTo
Example:
//A comment can belong to both an article and a video.
// Comment Modelclass Comment extends Model
{
public function commentable()
{
return $this->morphTo();
}
}
// Article Model
class Article extends Model
{
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
// Video Model
class Video extends Model
{
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Migration:Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->text('body');
$table->morphs('commentable'); // commentable_id ve commentable_type sütunlarını oluşturur
$table->timestamps();
});
morphedByMany
Example:
//Tags can be applied to both articles and videos.
// Tag Modelclass Tag extends Model
{
public function articles()
{
return $this->morphedByMany(Article::class, 'taggable');
}
public function videos()
{
return $this->morphedByMany(Video::class, 'taggable');
}
}
// Article Model
class Article extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
// Video Model
class Video extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
Migration:
Schema::create('taggables', function (Blueprint $table) {
$table->id();
$table->foreignId('tag_id')->constrained()->onDelete('cascade');
$table->morphs('taggable'); // taggable_id ve taggable_type sütunlarını oluşturur
$table->timestamps();
});
Laravel Eloquent ORM makes it very easy
to navigate relationships and work with related data. The
above-mentioned relationship types allow you to create flexible and
powerful relationships between database models.
Comments
Post a Comment