I have user profiles that allow users to leave messages for each other. (Think Facebook/MySpace)...
Given a profile_messages
table with the following fields id
, user_id
, author_id
, parent_id
, and message
, how would I efficiently display them in a threaded layout?
Note: The comments will only be 1 level deep.
Currently, I'm fetching all of the relevant comments, then rebuilding the collection to have a $messages
with a sub-collection of replies
on each item.
$messages = new Collection();
$replySets = [];
foreach ($user->profileMessages as $message)
{
$parentId = $message->parent_id;
if ($parentId == 0)
{
$messages->put($message->id, $message);
}
else
{
$replySets[$parentId][] = $message;
}
}
foreach ($replySets as $parentId => $replies)
{
$parent = $messages->get($parentId);
if (isset($parent))
{
if ( ! isset($parent->replies))
{
$parent->replies = new Collection();
}
foreach ($replies as $reply)
{
$parent->replies->push($reply);
}
}
}
// Pass $messages to the view
This works great. However, I can't help but to think there is a better way of doing this... Is there a better way to do this, or perhaps a way to make use of the relationships to get a result set matching the $profileMessage->replies
structure?