I have a table (Model3) that I am trying to query based on Model1 and Model2 relationships and a filter involved. I am trying to query all Model3 records given some Model1.id and Model2.status=0.
Model1 Model2 Model3
PK: id PK: id PK: id
FK: model1_id
FK: model3_id
status
Model1 can have many Model2s. A Model2 has a FK to a single Model3. Model3 is not related to Model2. I thought its (less) complicated to do this in two steps. So I defined a relation on Model1:
class Model1 {
...
public function Model2s() {
return $this->hasMany('Model2', 'model1_id')
->where('status', '=', '0' );
}
}
Then I would have to loop through the Model2 results and then get at the related Model3. So I created a relation on Model2 (half of a one-to-one relationship):
class Model2 {
...
public function Model3() {
return $this->belongsTo('Model3', 'model3_id' );
}
}
The problem being that Model2 can have 1000's of records to loop through to get it's Model3, which will be time consuming.
Is there a way to do this with a hasManyThrough relation on Model1 that takes into account I need a filter of Model2.status = 0? Then I can maybe use a single query to get at Model3?