Iterate through every row in 3 tables in one foreach loop - PHP - Laravel
<p>I've got 3 tables called <code>comments</code>, <code>post</code>, <code>feedback</code>.</p>

<p>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 </p>

<p><code>8:00 AM John has commented on this thread. 
9:00 PM Jack has posted a new post</code>.</p>

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

<p>My foreach loop right now is simply:</p>

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

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

foreach ($comments as $comment) {
    echo 'User has made a new comment: ' . $comment-&gt;comment;
}
</code></pre>

<p>As you can see I have got 3 <code>foreach</code> loops. I want something like </p>

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

<p>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 <code>Recent User Activity</code> page.</p>

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