I'm trying to load a Model with relationship in one SQL query. Is it possible?
For example, if i write classic eloquent query:
$user = User::find(1);
$user->hobby;
It will run 2 SQL queries: one for the User and another one for Hobby. And the User Model instance will include the hobby data in relations attribute. (The same will happen via ->with() method)
The closest i got to do this with one single SQL query is this:
$user = User::select(['users.*', 'hobbies.*'])
->join('hobbies', 'hobbies.id', 'users.hobbie_id')
->find(1);
But the problem is that the columns of Hobby will be in original attribute and mixing with User attributes instead of relations attribute as the first example.
Is it possible to do something like this but the relation data will be under to relations attribute?