As for Laravel to build relationships one child model through the other parent?

Good day!

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


Accordingly, in the parent model Project I prescribe the following relationships with the Client and Metrica:

class Project extends Model
{
 public function client()
{
 return $this->hasOne(Client::class);
}

 public function metrica()
{
 return $this->hasOne(Metrica::class);
}
}


Well, the Client and Metrica I prescribed standard attitude's belongsto()

class Client extends Model
{
 public function project()
{
 return $this->'s belongsto(Project::class);
}
}

Metrica class extends Model
{
 public function project()
{
 return $this->'s belongsto(Project::class);
}
}


Question: how to prescribe the relationship between the Client and Metrica?
April 7th 20 at 10:56
1 answer
April 7th 20 at 10:58
Solution
It is very easy.

Client:

hasMany(Metric::class, 'project_id', 'project_id')

Metric:

hasMany(Client::class, 'project_id', 'project_id')
@Valerie_Kihn54, really )) - Molly_Halvorson commented on April 7th 20 at 11:01

Find more questions by tags Laravel