Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dionarap's avatar

Showing different activity on timestamps

I am making an activity page, which will show the activity of a user and i'm wondering the best way to approach this issue of showing the different activity and ordering the activity on the created_at timestamps of all the tables merged.

at the moment i have

Controller

    public function getActivity()
      {
$blogs = Blogs::where('blogs.user_id', '=', Auth::User()->id)->orderBy('created_at', 'DESC')
        $comments = Comments::where('comments.user_id', '=', Auth::User()->id)- >orderBy('created_at', 'DESC')
$forumposts = Forumpost::where('forumposts.user_id', '=', Auth::User()->id)->orderBy('created_at', 'DESC')
         return view('myactivity')->withBlogs($blogs)->withComments($comments)->withForumposts($forumposts);

  }

View:

@foreach($blogs as $blog)
    <p>You posted {{$blog->blogname}} at {{$blog->created_at}}</p>

@endforeach 
@foreach($comments as $comment)
    <p>You posted {{$comment->comment}} at {{$comment->created_at}}</p>

@endforeach 
@foreach($forumposts as $forumpost)
    <p>You posted {{$forumpost->post}} at {{$forumpost->created_at}}</p>

@endforeach 

The issue with the above code is that it is not merged on a created_at is essentially three different created_at lists, how do i merge them into one?

0 likes
1 reply
rawilk's avatar
rawilk
Best Answer
Level 47

IMO that's not a good way to keep track of activity. It's just not maintainable, especially if you need to add more models in to the mix. You're better off using some kind of activity tracking table like this: https://github.com/spatie/laravel-activitylog

1 like

Please or to participate in this conversation.