Reverse-querying should work for you. Directly query the Lorem
model, and check for reverse-relationship existence:
Lorem::whereHas('bars', function($query) {
$query->whereHas('foos', function($subQuery) {
$subQuery->where('foos.id', 123);
})
})->get();
If Lorem
can alternatively be directly related to Foo
, then you can do an orWhereHas()
:
Lorem::whereHas('bars', function($query) {
$query->whereHas('foos', function($subQuery) {
$subQuery->where('foos.id', 123);
})
})->orWhereHas('foos', function($query){
$query->where('foos.id', 123);
})->get();
That should return all Lorem
models that belong to a Bar
that belongs to Foo
, or that directly belongs to Foo
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…