简体   繁体   English

获取关注者的帖子 laravel

[英]Get posts by followers laravel

I want to display a feed page for an authenticated user which shows the latest posts from users that they follow.我想为经过身份验证的用户显示一个提要页面,该页面显示他们关注的用户的最新帖子。 I have a follow system set up already which has the following:我已经设置了一个跟随系统,其中包含以下内容:

Tabels:标签:

  • Posts帖子
  • users用户
  • follow跟随

User model:用户模型:

 public function follow() {  
    return $this->BelongsToMany( 'User', 'Follow' ,'follow_user', 'user_id');
}

Feed controller:进给控制器:

public function feed () {

    $user = (Auth::user());

        return View::make('profile.feed')->with('user',$user);

    }

Feed.blade进给刀片

  @foreach ($user->follow as $follow)

 @foreach ($follow->posts as $post)

     //* post data here.

  @endforeach

 @endforeach

This is pulling in the posts from the users a user follows but, i have a problem.这是从用户关注的用户那里提取帖子,但是,我有一个问题。 The foreach is returning a user and then their posts each time. foreach 每次都返回一个用户,然后返回他们的帖子。

What its doing now:它现在在做什么:

Followed user 1关注用户 1

  • Post 1发布 1
  • Post 2帖子 2
  • Post 3 etc etc发布 3 等

Followed user 2关注用户 2

  • Post 1发布 1
  • Post 2帖子 2
  • Post 3 etc etc发布 3 等

What i would like to display:我想显示的内容:

  • Followed User 1 Post 1关注用户 1 发布 1
  • Followed User 2 Post 1关注用户 2 发布 1
  • Followed User 2 Post 2关注用户 2 发布 2
  • Followed User 1 Post 2 etc etc关注用户 1 发布 2 等

Any ideas?有任何想法吗?

<?php
        /**
         * Get feed for the provided user
         * that means, only show the posts from the users that the current user follows.
         *
         * @param User $user                            The user that you're trying get the feed to
         * @return \Illuminate\Database\Query\Builder   The latest posts
         */
        public function getFeed(User $user) 
        {
            $userIds = $user->following()->lists('user_id');
            $userIds[] = $user->id;
            return \Post::whereIn('user_id', $userIds)->latest()->get();
        }

First, you need to get the users that the current user follows and their ids so you can store it in $userIds .首先,您需要获取当前用户关注的用户及其ids以便将其存储在$userIds

Second, you need the feed to also contain your posts, So you add that to array too.其次,您需要提要也包含您的帖子,因此您也将其添加到数组中。

Third, You return the posts where the poster or author of that post is in that array that we got from the first step.第三,你返回岗位,其中posterauthor该职位的是该数组中,我们从第一步了。

And grab them store them from newest to oldest.并抓住它们从最新到最旧存储它们。

Any questions are welcome!欢迎任何问题!

Just a correction for Akar 's answer:只是对Akar答案的更正:
For those who are here in 2020, instead of lists you must use pluck .对于那些在 2020 年来到这里的人,您必须使用pluck而不是lists it changed in newer versions of laravel.它在较新版本的 Laravel 中发生了变化。

public function getFeed(User $user) 
        {
            $userIds = $user->following()->pluck('user_id');
            $userIds[] = $user->id;
            return \Post::whereIn('user_id', $userIds)->latest()->get();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM