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

时间:2017-05-21 11:06:13

标签: php mysql laravel orm eloquent

在我网站的某些控制器中,我必须编写所有这些sql查询以获得我需要的结果,因为我不认为/知道Laravel Eloquent 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相关的兴趣。

请注意,我将个人资料表与兴趣&使用数据透视中间表(interests_profiles和profiles_specialities)的专业表,我只将profile_id和interest_id / speciality_id作为外键。

我不知道是否有任何Laravel Eloquent方法可以完成这项工作,因为我需要根据他们对“WHERE”子句的兴趣过滤我的用户,例如:'WHERE intrests.name = Volleyball'?
如果没有,那么如何让查询只为每个用户运行一次,所以结果可能是这样的: -

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

然后我可以在视图中循环利益和专业。
我希望我能很好地解释这个问题,并为延长道歉而道歉。
提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您使用的是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中找到一种方法,因为它看起来像是一个非常灵活的框架。

相关问题