5

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.

What its doing now:

Followed user 1

  • Post 1
  • Post 2
  • Post 3 etc etc

Followed user 2

  • Post 1
  • Post 2
  • Post 3 etc etc

What i would like to display:

  • Followed User 1 Post 1
  • Followed User 2 Post 1
  • Followed User 2 Post 2
  • Followed User 1 Post 2 etc etc

Any ideas?

John
  • 196
  • 7
  • On your model, how did you set up the relationship? Take a look at [hasManyThrough](http://laravel.com/docs/4.2/eloquent#has-many-through) , it might be just what you need. – Victor Feb 22 '15 at 03:47

2 Answers2

11
<?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.

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.

And grab them store them from newest to oldest.

Any questions are welcome!

Akar
  • 5,075
  • 2
  • 25
  • 39
  • Thanks for the reply! This seem to be working, however i am just getting my data in the view: [{"id":118696,"category_id":"win","user":56031,"username": etc etc. How can i return the data to my view properly? – John Feb 22 '15 at 12:24
  • That function should be in either your model or your repository. And then you call that function from your controller store it in some variable and pass that through your `View::make()` function. – Akar Feb 22 '15 at 13:19
  • Vote up that answer if it helped you. ;) – Akar Feb 22 '15 at 13:23
  • Thanks for your help so far :) i am having a hard time calling the function from my controller, can you help? – John Feb 22 '15 at 18:55
  • Nothing because i am getting this error: Maximum function nesting level of '100' reached, aborting! – John Feb 22 '15 at 19:42
  • https://laracasts.com/forum/?p=2309-fatal-error-tests-not-finished-maximum-function-nesting-level-o/0 Check this out. – Akar Feb 22 '15 at 19:55
  • Sorry forgot to accept your answer. Got it working fine in the end. Thanks for your help! (I would voteup too but i don't have enough rep). – John Mar 06 '15 at 00:43
3

Just a correction for Akar's answer:
For those who are here in 2020, instead of lists you must use pluck. it changed in newer versions of laravel.

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

Pooria Honarmand
  • 773
  • 8
  • 17