【问题标题】:Laravel: How to query multiple indirectly-related tables?Laravel:如何查询多个间接相关的表?
【发布时间】:2017-05-21 11:06:13
【问题描述】:

在我网站的某个控制器中,我必须编写所有这些 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)将profiles 表与interests & specialities 表相关联,并且我只将profiles_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"}}]

然后我可以在视图中遍历兴趣和专长。
我希望我能很好地解释这个问题,我为拖延道歉。
提前致谢。

【问题讨论】:

    标签: php mysql laravel orm eloquent


    【解决方案1】:

    如果你使用 MySQL,你可以使用GROUP BYusers.id AND 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
    

    也许你也可以在 Laravel ORM 中找到一种方法,因为它看起来是一个非常灵活的框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 2021-12-10
      相关资源
      最近更新 更多