Php Laravel中的多线程评论,带雄辩

Php Laravel中的多线程评论,带雄辩,php,laravel,eloquent,threaded-comments,Php,Laravel,Eloquent,Threaded Comments,我有允许用户互相留言的用户配置文件。(想想Facebook/MySpace) 给定一个包含以下字段的profile\u messages表id,user\u id,author\u id,parent\u id,以及message,我如何在线程布局中有效地显示它们 注:注释深度仅为1级 目前,我正在获取所有相关的注释,然后重建集合,使其具有一个$messages,每个项目上都有一个子集合回复 $messages = new Collection(); $replySets = []; fore

我有允许用户互相留言的用户配置文件。(想想Facebook/MySpace)

给定一个包含以下字段的
profile\u messages
id
user\u id
author\u id
parent\u id
,以及
message
,我如何在线程布局中有效地显示它们

注:注释深度仅为1级

目前,我正在获取所有相关的注释,然后重建集合,使其具有一个
$messages
,每个项目上都有一个子集合
回复

$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


这很有效。然而,我忍不住想有更好的方法来做到这一点。。。有没有更好的方法来做到这一点,或者可能有一种方法可以利用这些关系来获得与
$profileMessage->repress
结构匹配的结果集?

根据您的描述,我假设您的根消息中有
user\u id
指向该用户的消息被发送到。以及对那些根消息的回复,这些根消息与
用户id
无关,而是与
父id
相关

因此:

关系如下:

// user model
public function messages()
{
  return $this->hasMany('Message')->where('parent_id', '=', 0);
  // you could also create RootMessage model with global scope instead of this where
}

// message model
public function replies()
{
  return $this->hasMany('Message', 'parent_id');
  // again, here you can create separate Reply model if you like
}

public function author()
{
  return $this->belongsTo('User', 'author_id');
}

public function user()
{
  return $this->belongsTo('User');
}

根据您的描述,我假设您的根消息具有
user\u id
指向该用户,消息已发送到。以及对那些根消息的回复,这些根消息与
用户id
无关,而是与
父id
相关

因此:

关系如下:

// user model
public function messages()
{
  return $this->hasMany('Message')->where('parent_id', '=', 0);
  // you could also create RootMessage model with global scope instead of this where
}

// message model
public function replies()
{
  return $this->hasMany('Message', 'parent_id');
  // again, here you can create separate Reply model if you like
}

public function author()
{
  return $this->belongsTo('User', 'author_id');
}

public function user()
{
  return $this->belongsTo('User');
}

根据您的描述,我假设您的根消息具有
user\u id
指向该用户,消息已发送到。以及对那些根消息的回复,这些根消息与
用户id
无关,而是与
父id
相关

因此:

关系如下:

// user model
public function messages()
{
  return $this->hasMany('Message')->where('parent_id', '=', 0);
  // you could also create RootMessage model with global scope instead of this where
}

// message model
public function replies()
{
  return $this->hasMany('Message', 'parent_id');
  // again, here you can create separate Reply model if you like
}

public function author()
{
  return $this->belongsTo('User', 'author_id');
}

public function user()
{
  return $this->belongsTo('User');
}

根据您的描述,我假设您的根消息具有
user\u id
指向该用户,消息已发送到。以及对那些根消息的回复,这些根消息与
用户id
无关,而是与
父id
相关

因此:

关系如下:

// user model
public function messages()
{
  return $this->hasMany('Message')->where('parent_id', '=', 0);
  // you could also create RootMessage model with global scope instead of this where
}

// message model
public function replies()
{
  return $this->hasMany('Message', 'parent_id');
  // again, here you can create separate Reply model if you like
}

public function author()
{
  return $this->belongsTo('User', 'author_id');
}

public function user()
{
  return $this->belongsTo('User');
}


谢谢,我一直在思考这些问题,但一直遇到+1问题,所以我知道我做得不对。我会试一试,看看这是否是我想要的急切地用n+1装载救援物资。是的,这就是你想要的:)嗯,不管我如何加载它们,我仍然遇到了n+1问题。。。我已经在这里发布了我根据您的代码所做的更改:不,您没有遇到n+1问题;)你只是在那里装了太多东西,但我不会担心的。n+1意味着对每个条目都运行一个查询,但这里不是这样。您只有
用户
关系和
作者
关系引用同一
用户
,但仍需要2次查询。就像在我的回答中一样-我想你不需要在
回复
上加载
用户
关系,因为它们指的是另一条消息,而不是用户自己,对吗?啊,你说的用户是多余的是对的。然而,我试图访问视图中的
$message->repress
,似乎是在试图在集合中还不存在它们时获取它们。这就是最后两个
select*from profile_messages
查询的来源……谢谢,我一直在思考这些问题,但一直遇到+1问题,所以我知道我做得不对。我会试一试,看看这是否是我想要的急切地用n+1装载救援物资。是的,这就是你想要的:)嗯,不管我如何加载它们,我仍然遇到了n+1问题。。。我已经在这里发布了我根据您的代码所做的更改:不,您没有遇到n+1问题;)你只是在那里装了太多东西,但我不会担心的。n+1意味着对每个条目都运行一个查询,但这里不是这样。您只有
用户
关系和
作者
关系引用同一
用户
,但仍需要2次查询。就像在我的回答中一样-我想你不需要在
回复
上加载
用户
关系,因为它们指的是另一条消息,而不是用户自己,对吗?啊,你说的用户是多余的是对的。然而,我试图访问视图中的
$message->repress
,似乎是在试图在集合中还不存在它们时获取它们。这就是最后两个
select*from profile_messages
查询的来源……谢谢,我一直在思考这些问题,但一直遇到+1问题,所以我知道我做得不对。我会试一试,看看这是否是我想要的急切地用n+1装载救援物资。是的,这就是你想要的:)嗯,不管我如何加载它们,我仍然遇到了n+1问题。。。我已经在这里发布了我根据您的代码所做的更改:不,您没有遇到n+1问题;)你只是在那里装了太多东西,但我不会担心的。n+1意味着对每个条目都运行一个查询,但这里不是这样。您只有
用户
关系和
作者
关系引用同一
用户
,但仍需要2次查询。就像在我的回答中一样-我想你不需要在
回复
上加载
用户
关系,因为它们指的是另一条消息,而不是用户自己,对吗?啊,你说的用户是多余的是对的。然而,我试图访问视图中的
$message->repress
,似乎是在试图在集合中还不存在它们时获取它们。这就是最后两个
select*from profile_messages
查询的来源……谢谢,我一直在思考这些问题,但一直遇到+1问题,所以我知道我做得不对。我会的