Php Laravel:如何查询多个间接相关的表?

Php Laravel:如何查询多个间接相关的表?,php,mysql,laravel,orm,eloquent,Php,Mysql,Laravel,Orm,Eloquent,在我网站的某个控制器中,我不得不编写所有这些sql查询来获得我需要的结果,因为我不认为/知道Laravel雄辩的ORM可以提供这样非常具体的东西 DB::select(' SELECT users.id, users.firstname, users.lastname, users.slug, profiles.bio, profiles.gender,

在我网站的某个控制器中,我不得不编写所有这些sql查询来获得我需要的结果,因为我不认为/知道Laravel雄辩的ORM可以提供这样非常具体的东西

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 
           ');
然而,当我返回这个查询的结果时,我得到了一个非常奇怪的结果,根据与profile.id关联的兴趣和专长的数量,查询将返回每个用户多次 类似于此:-

---------------------------------------------------------------------
| 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   |
---------------------------------------------------------------------
总之,我得到的是,查询在用户中循环的次数是他拥有与其个人资料id相关联的专长和兴趣的次数的数倍

请注意,我分别使用pivot中间表interests\u profiles和profiles\u specialities将profiles表与interests&specialities表链接,并且只将profiles\u id和interests\u id/speciality\u id作为外键放在它们上面

我不知道是否有任何Laravel雄辩的方法可以做到这一点,因为我需要使用WHERE子句根据用户的兴趣对其进行筛选,例如:“WHERE INTREST.name=BALLOOL”? 如果不是,那么如何让查询只对每个用户运行一次,因此结果可能是这样的:-

[{"users.id": 8, "users.firstname": 'Jhon', ...etc..., "interests":{"skydiving", "football"}, "specialities": {"Laravel", "JavaScript"}}]
然后我可以循环浏览视图中的兴趣和特长。 我希望我能很好地解释这个问题,并为拖延时间表示歉意。
提前感谢。

如果您使用的是MySQL,您可以使用GROUP BY users.id和GROUP_CONCAT,类似于:

SELECT users.id, 
    users.firstname, 
    users.lastname, 
    users.slug, 
    profiles.bio, 
    profiles.gender, 
    profiles.verified, 
    profiles.investor, 
    profiles.photo, 
    countries.name AS country, 
GROUP_CONCAT(DISTINCT interests.name) AS interests,
GROUP_CONCAT(DISTINCT 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
GROUP BY users.id
也许你也可以在Laravels ORM中找到一种方法,因为它看起来是一个非常灵活的框架