One-to-One Relationship:
users
id - integer
name - string
phones
id - integer
user_id - integer
In the User model:
public function phone()
{
return $this->hasOne(Phone::class);
}
In the Phone model:
public function user()
{
return $this->belongsTo(User::class);
}
One-to-Many Relationship:
posts
id - integer
name - string
comments
id - integer
post_id - integer
In the Post model:
public function comments()
{
return $this->hasMany(Comment::class);
}
In the Comment model:
public function post()
{
return $this->belongsTo(Post::class);
}
Many-to-many Relationship:
users
id - integer
name - string
roles
id - integer
name - string
role_user (Pivot table)
user_id - integer
role_id - integer
In User model
public function roles()
{
return $this->belongsToMany(Role::class, 'role_user');
}
In Role model:
public function users()
{
return $this->belongsToMany(User::class, 'role_user');
}
Important Note:
- The pivot table is always singular; if you give it another name, make it plural.
- The name of the pivot table is ordered alphabetically.
- If the pivot table has more than two foreign keys, you use it as a normal table and create a model for it.
One-to-One (Polymorphic):
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
In the Image model
public function imageable(): MorphTo
{
return $this->morphTo();
}
In the User model:
public function image(): MorphOne
{
return $this->morphOne(Image::class, 'imageable');
}
In the Post model:
public function image(): MorphOne
{
return $this->morphOne(Image::class, 'imageable');
}
One-to-Many (Polymorphic):
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
In the Comment model:
public function commentable(): MorphTo
{
return $this->morphTo();
}
In the Video model:
public function comments(): MorphOne
{
return $this->morphMany(Comment::class, 'commentable');
}
In the Post model:
public function comments(): MorphOne
{
return $this->morphMany(Comment::class, 'commentable');
}
Many-to-Many (Polymorphic):
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
In the Tag model:
public function videos(): MorphToMany
{
return $this->morphedByMany(Video::class, 'taggable');
}
public function posts(): MorphToMany
{
return $this->morphedByMany(Post::class, 'taggable');
}
In the Video model:
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
}
In the Post model:
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
}
Has One Through Relationship:
The "has-one-through" relationship defines a one-to-one relationship with another model.
mechanics
id - integer
name - string
cars
id - integer
model - string
mechanic_id - integer
owners
id - integer
name - string
car_id - integer
In the Mechanic Model:
public function carOwner(): HasOneThrough
{
return $this->hasOneThrough(Owner::class, Car::class);
}
Has Many Through Relationship:
The "has-many-through" relationship defines a many-to-many relationship with another model.
projects
id - integer
name - string
environments
id - integer
project_id - integer
name - string
deployments
id - integer
environment_id - integer
commit_hash - string
In the Project Model:
public function deployments(): HasManyThrough
{
return $this->hasManyThrough(Deployment::class, Environment::class);
}