Laravel hasMany relation count number of likes and comments on post

前端 未结 4 1882
刺人心
刺人心 2020-12-14 04:35

The code:

$posts = Jumpsite::find($jid)
            ->posts()
            ->with(\'comments\')
            ->with(\'likes\')
            ->with(\         


        
4条回答
  •  春和景丽
    2020-12-14 05:40

    Just single change to get results of the count

    In relation

     public function getTotalLikesAttribute(){
        return $this->hasMany('Like')->where('author_id',$this->author_id)->count();
    }
    

    In the view

    $post->getTotalLikesAttribute()
    

提交回复
热议问题