class Mechanic extends Model
{
public function carOwner()
{
return $this->hasOneThrough(
Owner::class,
Car::class,
'mechnic_id', // foreign key on cars tabiel
'car_id', // foreign key on owners table
'id', // local key on mechics table
'id' // local key on cars table
);
}
}
class Project extends Model
{
public function deployments()
{
return $this->hasManyThrough(
Deployment::class,
Environment::class,
'project_id', // foreign key on enviorments tabie
'environment_id', // foreign key on deployments table
'id', // local key on projects table
'id' // local key on environments table
);
}
}
class Image extends Model
{
public function imageable()
{
return $this->morphTo(
__FUNCTION__,
'imageable_type',
'imageable_id',
);
}
}
class Post extends Model
{
// this method on user model
public function image()
{
return $this->morphOne(
Image::class,
'imageable',
'imageable_id',
'id'
);
}
}
.
comment ----------- video
\ /
comment ------=
/ \
comment --- post
class Comment extends Model
{
public function commentable()
{
return $this->morphTo(
__FUNCTION__,
'commentable_type',
'commentable_id'
);
}
}
class Post extends Model
{
// same method on video model
public function comments()
{
return $this->morphMany(
Comment::class,
'commentable'
'commentable_type',
'commentable_id'
'id'
);
}
}
.
video
/
tag -------------- video
\ / \
tag -------= video
/ \ post
tag \ /
----- post
\
post
class Tag extends Model
{
public function posts()
{
return $this->morphedByMany(
Post::class,
'taggable'
);
}
public function videos()
{
return $this->morphedByMany(
Video::class,
'taggable'
);
}
}
class Post extends Model
{
// same method on video model
public function tags()
{
return $this->morphMany(
Tag::class,
'taggable'
);
}
}