TDD Forum: User Activities

Published 2 months ago by rixgov

Has anybody had any luck creating a feed with either all users, or all users except the current user? Just can't seem to get it to work with all the information already in MYSQL. Any suggestions?

Activity class:

public static function allFeed($user, $take = 50) { $otherUsers = $user->where('id', '!=', Auth::id())->get();

     return static::where('user_id', $otherUsers)
           ->latest()
           ->with('subject')
           ->take($take)
           ->get()
           ->groupBy(function ($activity) {
          return $activity->created_at->format('Y-m-d');
     });

}

jaydeluca

I don't think you can pass a collection of user objects to the where like that.

You might be able to do something like

public static function allFeed($user, $take = 50) { 

    $otherUsers = $user->where('id', '!=', Auth::id())->get()->pluck('id');

     return static::whereIn('user_id', $otherUsers)
           ->latest()
           ->with('subject')
           ->take($take)
           ->get()
           ->groupBy(function ($activity) {
          return $activity->created_at->format('Y-m-d');
     });
}
rixgov

What if i passed the IDs over as a collection? ... just unsure of how to key it

     $otherUsers = $user->where('id', '!=', Auth::id())->get()->pluck('id');
     foreach($otherUsers as $otherUser) {
          return Activity::feed($otherUser, 50);
jaydeluca

I'm a little confused about what "Activity" is and how it relates to the user. Can you give a little more overview of what you are trying to do? And you say it's not working, can you show an example of what the output is?

rixgov

Oh it's actually right out of Jeffrey's Forum TDD series

Here's the GitHub files.... activity is located in the App directory

Thanks :)

rixgov

It seems like there must be a way to manipulate ProfilesController to cycle through all the activity, since Activity accepts each individual user, takes the id and matches that against the activities table. I just can't figure out where I'm going wrong. I should be able to simply cycle through each activity in the create_activities table, match/bind the user_id table to the Users table and create a list of activities by their owner.

Sign In or create a forum account to participate in this discussion.