I'm currently working on laravel framework and I'm stuck with some relations and eager loading issues.
Situation
I have three models A, B and C
I have two relations
- A has many B
- B has many C
By default (using the $with attribute in Model) :
- A doesn't include B
- B include C
So most of the time I'm using A without B and B with C
And here is how I've set up the relationship methods & eager loading
class A extends Model {
...
protected $with = [];
public function bs() {
return $this->hasMany('App\Models\B');
}
}
class B extends Model {
...
protected $with = ['cs'];
public function cs() {
return $this->hasMany('App\Models\C');
}
public function a() {
return $this->belongsTo('App\Models\A');
}
}
class C extends Model {
...
public function b() {
return $this->belongsTo('App\Models\B');
}
}
Problem
For a specific task I'd like to query A with all B and without any C
When I'am using A::query()->with('b')
C are loaded by default
So I'am trying to use A::query()->with('b')->without('b.c')
But it keep loading B to C relations.
Have you any idea on how to achieve this ?
Thanks for your help !