【问题标题】:Hot to get the posts that belongs to users that other user follows热门获取属于其他用户关注的用户的帖子
【发布时间】:2020-06-27 15:40:14
【问题描述】:

我有 3 张桌子 usersfollowposts 用户表

id
...

跟随表格

user_id
designer_id

帖子表

user_id
...

如何获取用户关注的设计师的帖子?

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    使用hasManyThrough,它通过中间关系提供对远距离关系的访问。

    定义如下关系:

    用户模型

     /**
     * Get all of the posts of followed users.
     */
    public function followedPosts()
    {
        return $this->hasManyThrough(
                      Post::class,
                      Follow::class,
                      'user_id',
                      'user_id',
                      'id',
                      'designer_id');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-10
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多