How to get posts of followed users

Posted 1 year ago by agilasadi

I'm trying to make news feed page, but in this page I need to output posts ordered by date and also they must belong to the user, which authed user follows. I tried a few things and than realised that I have the wrong logic. so in user model I have:

function followers()
{
    return $this->belongsToMany('App\User', 'followers', 'user_id', 'follower_id');
}

function follows()
{
    return $this->belongsToMany('App\User', 'followers', 'follower_id', 'user_id');
}

function articles(){
   return $this->hasMany('App\Article');
}

and than in article I have the relationships:

public function user()
{
    return $this->belongsTo('App\User');
}

I don't know if I can simplly get all articles and than output the ones the user follows. Any help would be usefull

Please sign in or create an account to participate in this conversation.