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

kachi_dk's avatar

Get all the users and their businesses in one query with a relationship

I have a relationship User->Business. I want to get all the row of the user and the business in one query. i.e. $query = User->Business, should give me something like this.

[
user=>[
		....
	]
business =>[
		...
	]
]

I want to get all the users and their businesses in one query

0 likes
3 replies
kokoshneta's avatar

If each user has (or can have) more than one business, you cannot get users with all their related business in one query – SQL doesn’t work like that. You’ll need two queries: one to fetch the users, and one to fetch the businesses. Nakov’s answer is the easy Eloquent way to do this in a single line, but it does run two queries under the hood.

Please or to participate in this conversation.