Laravel join with 3 Tables(Laravel 加入 3 个表)
问题描述
我正在构建一个类似 Twitter 的应用.有一个供稿,我只想在其中显示我关注的用户的帖子.
I am building a Twitter-like app. There is a Feed in which I want to only show posts of Users who I follow.
我尝试了所有连接,但似乎没有任何效果.
I tried everything with joins, but nothing seems to work.
我有 3 个表:Users
、Followers
、Shares
表格如下所示:
用户:id
关注者:user_id
、follower_id
分享:user_id
我需要得到的是所有共享 WHERE share.user_id = follower.follower_id"ANDWHERE follower.user_id = users.id"
What I need to get is "ALL Shares WHERE share.user_id = followers.follower_id" "ANDWHERE followers.user_id = users.id"
假设 users.id 是 3,我试过这个:
Assume, the users.id is 3, I tried this:
$shares = DB::table('shares')
->leftjoin('followers', 'shares.user_id', '=', 'followers.follower_id')
->leftjoin('users', 'followers.user_id', '=', 'users.id')
->where('users.id', 3)
->where('shares.user_id', 'followers.follower_id')
->get();
但它不起作用.
感谢任何帮助:)
推荐答案
我认为你的加入是错误的:
I believe your join is wrong:
$shares = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('followers', 'followers.user_id', '=', 'users.id')
->where('followers.follower_id', '=', 3)
->get();
我还建议您将表命名为 follows
代替,说 user 通过关注有很多关注者
和 user 有很多关注者感觉更自然关注通过关注
.
I also suggest you to name your table as follows
instead, it feels a bit more natural to say user has many followers through follows
and user has many followees through follows
.
示例
$shares = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('follows', 'follows.user_id', '=', 'users.id')
->where('follows.follower_id', '=', 3)
->get();
模型方法
我没有意识到您使用的是 DB::
查询而不是模型.所以我正在修复答案并提供更多清晰度.我建议你使用模型,对于那些从框架开始,特别是 SQL 的人来说,它会容易得多.
Model approach
I didn't realize you were using DB::
queries and not models. So I'm fixing the answer and providing a lot more clarity. I suggest you use models, it's a lot easier for those beginning with the framework and specially SQL.
模型示例:
class User extends Model {
public function shares() {
return $this->hasMany('Share');
}
public function followers() {
return $this->belongsToMany('User', 'follows', 'user_id', 'follower_id');
}
public function followees() {
return $this->belongsToMany('User', 'follows', 'follower_id', 'user_id');
}
}
class Share extends Model {
public function user() {
return $this->belongsTo('User');
}
}
模型使用示例:
$my = User::find('my_id');
// Retrieves all shares by users that I follow
// eager loading the "owner" of the share
$shares = Share::with('user')
->join('follows', 'follows.user_id', '=', 'shares.user_id')
->where('follows.follower_id', '=', $my->id)
->get('shares.*'); // Notice the shares.* here
// prints the username of the person who shared something
foreach ($shares as $share) {
echo $share->user->username;
}
// Retrieves all users I'm following
$my->followees;
// Retrieves all users that follows me
$my->followers;
这篇关于Laravel 加入 3 个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 加入 3 个表
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01