Answer the question
In order to leave comments, you need to log in
How in Laravel to build relationships of one child model with another through the parent?
Good afternoon!
I have three tables:
projects
id - integer
name - string
clients
id - integer
project_id - integer
name - string
metricas
id - integer
project_id - integer
title - string
class Project extends Model
{
public function client()
{
return $this->hasOne(Client::class);
}
public function metrica()
{
return $this->hasOne(Metrica::class);
}
}
class Client extends Model
{
public function project()
{
return $this->belongsTo(Project::class);
}
}
class Metrica extends Model
{
public function project()
{
return $this->belongsTo(Project::class);
}
}
Answer the question
In order to leave comments, you need to log in
Very easy.
Client:
hasMany(Metric::class, 'project_id', 'project_id')
Metric:
hasMany(Client::class, 'project_id', 'project_id')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question