Laravel 加入 3 个表 [英] Laravel join with 3 Tables

查看:32
本文介绍了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 个表:UsersFollowersShares

表格如下所示:

用户:id

关注者:user_idfollower_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 个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆