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
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.