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
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.
@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 sign in or create an account to participate in this conversation.