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 个表
基础教程推荐
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01