Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

richbreton's avatar

How to get this eloquent query to return a builder object

So I have a model with a few hasMany() relationships and I as is normal It returns fine with a collection, since Eloquent rides on top of builder can I get it to return a builder object so other people can chain what they want to it and call get()?

Here is My Query:

    return Company::findOrFail($id)->load('contacts', 'addresses', 'team');

The idea is to put this in a method and have someone just able to take it and add anything else they like before call get()

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

try

return Company::with('contacts', 'addresses', 'team')->where('id',$id);

Please or to participate in this conversation.