What you are referring to are called joins https://laravel.com/docs/8.x/queries#joins
These work with eloquent as well. But they aren't relationships
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi all,
No idea if my title makes any sense.
But I would like to know if this is possible with eloquent please:
ModelA::select(
'id',
)
->with('ModelB:id,name')
->with('ModelC:id,name')
->with(['ModelD' => static function ($query){
$query->where('ModelD.id','=', 'ModelB.id');
$query->where('ModelD.id2','=', 'ModelC.id');
}])
->get();
In short, is there a way to use the value(id) of ModelB and ModelC(id) to query ModelD(2 id columns) in a relation? As I would like ModelD to return a column(image_url) when the IDs of both ModelB and ModelC are found in a ModelD row.
I am struggling to explain this in clear English sorry.....
What you are referring to are called joins https://laravel.com/docs/8.x/queries#joins
These work with eloquent as well. But they aren't relationships
Please or to participate in this conversation.