How can i directly get data of Model3
staying from Model1
like hasMayThrough
?
I have 3 models and 4 tables
Model 1 |
---|
id |
model2_id |
Model 2 |
---|
id |
Model 3 |
---|
id |
PivotTable for Model2 and Model3 |
---|
model2_id |
model3_id |
3
Here, You have pivot table means it is many-to-many relationship. In that case you need to use belongsToMany
in model2 like below
public function model3relation(): BelongsToMany
{
return $this->belongsToMany(Model3::class);
}
then you can use
Model1->find(1)->model2->model3relation()->get()
1