Displaying posts of users the user follows through Laravel relationships

James Parsons

I would like to display the posts of everyone the current user follows, ordered by date desc.

I have a many to many relationship supplying all the people the user is following.

$users = User::find(Auth::user()->id)->follow()->get();

I have a one to many relationship displaying the posts for any user.

$updates = App\User::find(?????)->updates()->orderBy('created_at', 'desc')->get();

The question mark's shows where the followers ID's need to be placed.

I can put the above query inside the for each loop but that obviously works its way through each follower rather than all posts in date order.

I suspect I may need to set a new relationship and work from the beginning. Can anyone advise.

User Model

     public function updates()
    {
        return $this->hasMany('App\update');
    }

/**
 * User following relationship
 */
 // Get all users we are following
public function follow()
{
  return $this->belongsToMany('App\User', 'user_follows', 'user_id', 'follow_id')->withTimestamps()->withPivot('id');;;
}
// This function allows us to get a list of users following us
public function followers()
{
  return $this->belongsToMany('App\User', 'user_follows', 'follow_id', 'user_id')->withTimestamps();;
}

}

Update Model

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

Thank you.

patricus

Since you want the posts, it is probably going to be easier starting a query on the Post model, and then filter the posts based on their relationships.

Assuming your Post model has an author relationship to the User that created the post, and the User has a follower relationship to all the Users that are following it, you could do:

$userId = Auth::user()->id;
$posts = \App\Post::whereHas('author.follower', function ($q) use ($userId) {
        return $q->where('id', $userId);
    })
    ->latest() // built in helper method for orderBy('created_at', 'desc')
    ->get();

Now, $posts will be a collection of your Post models that were authored by a user that is being followed by your authenticated user.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Retrieving posts of all users that a user follows - rails - ActiveRecord

From Dev

Displaying a User's Posts by Category

From Dev

Laravel: search through relationships?

From Dev

Loop through relationships Laravel

From Dev

displaying message users in Laravel

From Dev

Displaying users in query result if they have no posts?

From Dev

Displaying users in query result if they have no posts?

From Dev

Displaying messages to users through class

From Dev

Laravel Auth::user() relationships

From Dev

Wordpress posts not displaying through dedicated url

From Dev

setting up gravitars for my posts through users

From Dev

Laravel displaying different posts on a different pages

From Dev

How to query user with posts in Laravel

From Dev

Accessing Properties Through Relationships in Laravel 5.0

From Dev

Laravel, querying through many-to-many relationships

From Dev

Laravel displaying all posts with WHERE clause and pagenation in laravel

From Dev

Laravel posts and tags: has many through relationship?

From Dev

Displaying posts made by user that is logged in - c#, asp.net

From Dev

Passing user input through a class and displaying it in the console

From Dev

Is this a script to help users to add new posts through a GUI?

From Dev

Laravel - Get an user who has specific relationships

From Dev

Users relationships with other users

From Dev

Postgres Query: Ranking posts by users based on user activity

From Dev

how to delete all users and posts based on 'user_meta'?

From Dev

How to prevent users from editing or deleting other user posts ? Django

From Dev

Create relationships between addreses and users. Return it on the view on Laravel

From Dev

Laravel 5, authenticating and displaying user roles

From Dev

displaying navbar according to user roles and permisions laravel

From Dev

Displaying multiple users from http data instead of a single user

Related Related

  1. 1

    Retrieving posts of all users that a user follows - rails - ActiveRecord

  2. 2

    Displaying a User's Posts by Category

  3. 3

    Laravel: search through relationships?

  4. 4

    Loop through relationships Laravel

  5. 5

    displaying message users in Laravel

  6. 6

    Displaying users in query result if they have no posts?

  7. 7

    Displaying users in query result if they have no posts?

  8. 8

    Displaying messages to users through class

  9. 9

    Laravel Auth::user() relationships

  10. 10

    Wordpress posts not displaying through dedicated url

  11. 11

    setting up gravitars for my posts through users

  12. 12

    Laravel displaying different posts on a different pages

  13. 13

    How to query user with posts in Laravel

  14. 14

    Accessing Properties Through Relationships in Laravel 5.0

  15. 15

    Laravel, querying through many-to-many relationships

  16. 16

    Laravel displaying all posts with WHERE clause and pagenation in laravel

  17. 17

    Laravel posts and tags: has many through relationship?

  18. 18

    Displaying posts made by user that is logged in - c#, asp.net

  19. 19

    Passing user input through a class and displaying it in the console

  20. 20

    Is this a script to help users to add new posts through a GUI?

  21. 21

    Laravel - Get an user who has specific relationships

  22. 22

    Users relationships with other users

  23. 23

    Postgres Query: Ranking posts by users based on user activity

  24. 24

    how to delete all users and posts based on 'user_meta'?

  25. 25

    How to prevent users from editing or deleting other user posts ? Django

  26. 26

    Create relationships between addreses and users. Return it on the view on Laravel

  27. 27

    Laravel 5, authenticating and displaying user roles

  28. 28

    displaying navbar according to user roles and permisions laravel

  29. 29

    Displaying multiple users from http data instead of a single user

HotTag

Archive