Sunday, May 21, 2017

Laravel: How to query multiple indirectly-related tables?

In some controller in my website I had to write all this sql query to get the results that I need since I don't think / know that Laravel Eloquent ORM can provide something very specific like this

DB::select('SELECT users.id, 
            users.firstname, 
            users.lastname, 
            users.slug, 
            profiles.bio, 
            profiles.gender, 
            profiles.verified, 
            profiles.investor, 
            profiles.photo, 
            countries.name AS country, 
            interests.name AS interests, 
            specialities.name AS specialities 
            FROM users 
            JOIN profiles ON profiles.user_id = users.id 
            JOIN countries ON profiles.country_id = countries.id 
            JOIN interests_profiles ON interests_profiles.profile_id = profiles.id 
            JOIN interests ON interests_profiles.interest_id = interests.id 
            JOIN  profiles_specialities ON profiles_specialities.profile_id = profiles.id 
            JOIN specialities ON profiles_specialities.speciality_id = specialities.id 
           ');

However, When i return-ed the results of this query i got a very weird results where the query will return each user multiple times depending on the number of the (interests & specialities) that is associated with his profile.id Something almost similar to this:-

---------------------------------------------------------------------
| users.id | users.firstname | ...etc... | interests | specialities |
---------------------------------------------------------------------
|    8     |      Jhon       | ...etc... | skydiving |   Laravel    |
---------------------------------------------------------------------
|    8     |      Jhon       | ...etc... | football  |  JavaScript  |
---------------------------------------------------------------------   
|    10    |      Daved      | ...etc... | Chatting  |   Physics    |
---------------------------------------------------------------------
|    10    |      Daved      | ...etc... |  Driving  | Engineering  |
---------------------------------------------------------------------
|    11    |      Steve      | ...etc... |  Writing  |  Woodworks   |
---------------------------------------------------------------------

So in summary what I got is that the query loops through the user many times as much as he have specialities & interests associated with his profile id.

Note that I linked the profiles table with the interests & specialities tables using pivot mid tables (interests_profiles and profiles_specialities) respectively, And I put only on them profiles_id and interest_id/speciality_id as foreign keys.

I don't know if there is any Laravel Eloquent way to get this done, because I need to filter my users based on their interests with "WHERE" clause, for example: 'WHERE intrests.name = Volleyball'?
If not, Then how to get the query to run one time only per user, so the results could be something like this:-

[{"users.id": 8, "users.firstname": 'Jhon', ...etc..., "interests":{"skydiving", "football"}, "specialities": {"Laravel", "JavaScript"}}]

And then I can loop through interests and specialities in the view.
I hope that i explained the problem well, And i apologise for prolongation.
Thanks in advance.



via Hamed Adil

Advertisement