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

hiddenlucas's avatar

Two different tables in one loop.

Hello,

I would like to display the values ​​of two tables in one loop sorting after the creation date. It is a mix of comments and notifications. Something like this (order by created_at) ::

@loop
#comment 1
#comment 2
#notification 1
#comment 3
#notification 2

my tables loo like this

---comments
id|user_id|ticket_id|comment|created_at|updated_at
---ticket_notifications
id|ticket_id|status|created_at|updated_at

I use

$collection = collect($comments, $ticket_notification);
$collapsed = $collection->collapse();
$collapsed->all();

but return me empty array

0 likes
2 replies
manelgavalda's avatar

The collect() helper just accepts one parameter, so you are just "collecting" the $comments. I suggest you to use merge() instead:

$collapsed = $comments->merge($ticket_notification);
$collapsed->sortBy('created_at'); // If you want the collection sorted by creation date.
hiddenlucas's avatar

@GLOBALS - merge doesn't work for me, but this work fine

$arr = collect([$comments, $ticket_notification])->collapse()->keyBy('created_at')->sortKeys();

thanks for hints.

Please or to participate in this conversation.