【问题标题】:Problem with getting comments, replies and child replies of post获取帖子的评论、回复和子回复的问题
【发布时间】:2019-09-26 04:42:11
【问题描述】:

制作像 miniinstagram 这样的应用程序,我们有推荐(帖子)和可以评论它们的用户。通过api获取数据。并且在获取所有 cmets 和某些帖子的所有回复时遇到问题。

我有一个数据库表

comment
-------------------------------
|id                           |
|text(comment text)           |
|recommendation_id(post_id)   |
|user_id(author)              |
|parent_id(parent comment id) |
-------------------------------

在 parent_id 中我们可以设置父评论 id 。评论有回复,回复也有回复。 这里评论模型

class Comment extends Model
{
    protected $with = ['user:id,name'];
    protected $table = 'comments';
    protected $fillable = ['text','user_id','recommendation_id'];



    public function recommendation()
    {
        return $this->belongsTo('App\Models\MobileApp\Recommendation' , 'recommendation_id');
    }
    public function user()
    {
        return $this->belongsTo('App\User');
    }
    public function replies()
    {
        return $this->hasMany('App\Models\MobileApp\Comment' ,'parent_id');
    }
    public function parent()
    {
        return $this->belongsTo('App\Models\MobileApp\Comment' ,'id');
    }
}

和 推荐模型

class Recommendation extends Model
{
    protected $table = 'recommendations';
    protected $fillable = ['course_id','title','description','file'];

    public function comments()
    {
        return $this->hasMany('App\Models\MobileApp\Comment')->where('parent_id', '=', Null);
    }
}

这里推荐控制器方法索引

public function index()
    {
        $recommendations = Recommendation::all();
        foreach($recommendations as &$item){
            //changing to full path
            $item['file'] = 'https://example.com/' . $item['file'];
            //getting comments
            $item['comments']=$item->comments;
           //getting replies
            foreach($item->comments as $alphabet => $collection) {
                 $collection->replies;
//if reply array not empty then look for child repliese. I should have here recursive 
                    if($item->comments[$alphabet]->replies->count()>0){
                    foreach($item->comments[$alphabet]->replies as $alphabet => $collection) {
                        $collection->replies;
                    }
                }
            }

        }
        if ($recommendations)
        {
            $response = ['success'=>true, 'data'=>$recommendations];
        }
        else
            $response = ['success'=>false, 'data'=>'Record doesnt exists'];


        return $response;
    }

问题是如果我得到的回复不是空的,那么我必须寻找孩子的回复。我应该有类似 recursive 的东西。我该怎么做?

更新: 你好。我在帖子、cmets 和回复中添加了设置喜欢功能。并且有一些问题。我有“喜欢”表,它有 ['user_id','likeobject_id']。 Comment 和 User 之间是多对多的关系。我已添加到 Comment 模型中

public function CommentLikesCount()
{
    return $this->belongsToMany('App\User','likes' ,'likeobject_id','user_id')->withPivot('likeobject')->where('likeobject', '=', 2);
}

这里 likeobject 只是意味着它是评论而不是帖子。没关系 。 并添加到 User 模型中

public function CommentLikes()
{
    return $this->belongsToMany('App\Models\MobileApp\Comment','likes' ,'user_id','likeobject_id')->withPivot('likeobject')->where('likeobject', '=', 2);
}

然后添加到

 foreach ($recommendation->comments as $comment) {
        $comment->loadRecursiveReplies();

        $comment['comment_likes']=$comment->CommentLikesCount->count();

    }

但它只显示第一条评论的点赞数。供家长评论。不用于回复。

我添加了这个。但它不起作用。

public function loadReplies() {
    return $this->load('replies.CommentLikesCount');
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你的代码应该是这样的:

    控制器:

        ...
    
        public function index()
        {
            $recommendations = Recommendation::with([
                'comments.user',
                'comments.replies.user'
            ])->paginate(10);
    
            foreach ($recommendations as $item) {
    
                foreach ($item->comments as $comment) {
                    // I have moved your code to load `replies` in model
                    $comment->loadRecursiveReplies();
                }
    
            }
    
            if (!$recommendations->isEmpty()) {
                $response = ['success' => true, 'data' => $recommendations->toArray()];
            } else {
                $response = ['success' => false, 'data' => 'Record doesnt exists'];
            }
    
    
            return $response;
        }
    
        ...
    

    评论模型:

    class Comment extends Model
    {
         // All the code that loads replies recursively is here...
         ...
    
         public function isRepliesLoaded() {
              return isset($this->relations['replies']);
         }
    
         public function loadReplies() {
              return $this->load('replies.user');
         }
    
         public function loadRecursiveReplies()
         {
            if (!$this->isRepliesLoaded()) {
                $this->loadReplies();
            }
    
            if ($this->replies->isEmpty()) {
                return;
            }
    
            foreach ($this->replies as $reply) {
                $reply->loadRecursiveReplies();
            }
        }
    
         ...
    }
    

    推荐模型:

    class Recommendation extends Model
    {
        ...
        // This will prepend site url in your file path when you access with `$this->file` automatically.
        public function getFileAttribute($value) {
            return 'https://example.com/' . $value;
        }
        ...
    }
    

    PS: 此代码未经测试。

    【讨论】:

    • 它在第一次尝试时就像一个魅力。你是天才。感谢您的时间。下次我会学习你的代码来做这件事。谢谢
    • 很高兴帮助你@Rock :)
    • 你好。我在您的代码中添加了 Likes 功能并且有一些问题。你能告诉我如何解决它吗?
    • @Rock - 发布一个新问题并链接此帖子。我或来自stackoverflow 的人会帮助你。在一个线程中回答多个问题是不对的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多