【问题标题】:Laravel 6 Eloquent - Relationship between three tables and pivotsLaravel 6 Eloquent - 三个表和枢轴之间的关系
【发布时间】:2020-05-06 09:37:42
【问题描述】:

我有三个表,我想关联这三个表,但我不知道如何。

我有一个用户表和两个帖子和类别表,类别也将类别和子类别存储在同一个表上(使用类型列分隔)。

帖子和用户也有一个数据透视表以及帖子和类别。但我想通过以下类别获取用户帖子:

类别或子类别 -> 帖子 -> 用户

类别模型:

class Category extends Model
{

    protected $fillable = ['name', 'description', 'type', 'category_id','post_id'];

    public function posts() {

        return $this->belongsToMany(Post::class)->withPivot('post_id');

    }
}

后模型:

class Post extends Model {

    public function users() {
        return $this->belongsToMany(User::class, 'post_user','post_id', 'user_id')->withTimestamps();
    }


    public function categories() {
        return $this->belongsToMany(Category::class)->withPivot('category_id');
    }

用户模型:


class User extends Authenticatable {

    public function posts() {
        return $this->belongsToMany(Post::class, 'post_user', 'user_id', 'post_id')->withTimestamps();
    }

然后在控制器中我只有这个

$posts = Category::with('posts')->get();

我不知道如何关联这 3 个表格。也许我只需要在 3 个表之间创建一个数据透视表?

编辑:感谢 Qirel 帮助我关联表格:

Category::with('posts.users');

然后我只想显示特定用户发布帖子的类别,例如

Category::with('posts.users')->where('user.id',1);

有可能吗?

【问题讨论】:

  • 您可以嵌套关系,例如Category::with('posts.users');(在postsposts.users 关系上都渴望加载)。用户与帖子相关,所以应该有一个post_user 数据透视表,并且一个类别链接到一个帖子,所以应该有一个category_post 数据透视表。通过将这些组合在一起,您可以找出哪些用户在哪些类别中发布过帖子,反之亦然。
  • 那么我可以显示用户发帖的类别吗?喜欢post.users where id = 1?
  • 是的,Category::with('posts.users')->whereHas("posts.users", function($query) { $query->where("id", 1); });Category::with('posts.users', function($query) { $query->where("id", 1); });(一个只是过滤关系数据并选择类别,另一个检查是否有关系数据)。
  • 在两个选项中都返回这个:SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `categories` where exists (select * from `posts` inner join `category_post` on `posts`.`id` = `category_post`.`post_id` where `categories`.`id` = `category_post`.`category_id` and exists (select * from `users` inner join `post_user` on `users`.`id` = `post_user`.`user_id` where `posts`.`id` = `post_user`.`post_id` and `id` = 1)))
  • category_postpost_user 中是否有id 列?

标签: php laravel eloquent pivot-table


【解决方案1】:

正如 cmets 中所讨论的,这可以通过嵌套关系来实现。

通过使用with() 使用预先加载来“预加载”关系数据,并通过使用whereHas() 和您想要的过滤器来“预加载”关系条件,确保您只获取存在匹配关系的记录条件 - 在您的情况下,它只会加载特定用户发布的类别。

$user_id = 1;
Category::with('posts.users')
        ->whereHas("posts.users", function($query) use ($user_id) { 
            $query->where("id", $user_id); 
        })
        ->get();

您还可以获取所有类别(无论提供的用户是否已发布),但将关系数据限制为仅来自该用户(这意味着您可以获得所有类别,但仅获取 ID 为 1 的用户所在的帖子)海报)。您可以通过在急切加载上设置条件来实现这一点(请注意,与上面使用 whereHas() 不同,现在这是一个键/值配对数组)。

$user_id = 1;
Category::with(['posts.users' => function($query) use ($user_id) { 
            $query->where("id", $user_id); 
        }])
        ->get();

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 2022-01-09
    • 2018-05-16
    • 2014-10-23
    • 1970-01-01
    • 2021-03-06
    • 2019-07-05
    • 1970-01-01
    相关资源
    最近更新 更多