在一个foreach循环中迭代3个表中的每一行 - PHP - Laravel

I've got 3 tables called comments, post, feedback.

In a profile page, a user can see someone's activity. So for instance, if they make a comment and a post, they can see something like

8:00 AM John has commented on this thread. 9:00 PM Jack has posted a new post.

The problem is that I don't know how can I combine all these records in one foreach loop.

My foreach loop right now is simply:

foreach ($posts as $post) {
    echo 'User has made a new post: ' . $post->post;
}

foreach ($feedback as $feed) {
    echo 'User has given a new feedback: ' . $feedback->feedback;
}

foreach ($comments as $comment) {
    echo 'User has made a new comment: ' . $comment->comment;
}

As you can see I have got 3 foreach loops. I want something like

foreach ($activities as $activity) {
    if ($activity->type == 'comment')
        echo "User can made a new comment: $activity->comment";
    if ($activity->type == 'post)
        echo "User has made a new post: $activity->post";
}

I hope it makes sense. Basically, I want to combine all three tables as one and then make a show them in a foreach loop in a Recent User Activity page.

Please let me know wha you think the best practice for this is. Thanks!

I would leverage Eloquent's polymorphic relationships for this. Essentially your three tables have one thing in common, in that they are all activities, or 'Activitiable' for lack of a better term. See: https://laravel.com/docs/master/eloquent-relationships#polymorphic-relations