gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:48:55 26 4
gpt4 key购买 nike

在我网站的某个 Controller 中,我必须编写所有这些 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_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"}}]

然后我可以在 View 中循环浏览兴趣和专业。
我希望我能很好地解释这个问题,对于拖延我深表歉意。
提前致谢。

最佳答案

如果您使用的是 MySQL,则可以使用 GROUP BY users.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

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

关于php - Laravel:如何查询多个间接相关的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44096113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com