Laravel hasMany count count the number of likes and comments on the post

Code:

$posts = Jumpsite::find($jid) ->posts() ->with('comments') ->with('likes') ->with('number_of_comments') ->with('number_of_likes') ->where('reply_to', 0) ->orderBy('pid', 'DESC') ->paginate(10); 

Each post has a comment and likes. First, I show a few comments to avoid heavy loads. But I want to show how many total comments and comments for each post. How to do it?

Model Code:

 public function likes() { return $this->hasMany('Like', 'pid', 'pid'); } public function comments() { return $this->hasMany('Post', 'reply_to', 'pid')->with('likes')->take(4); } public function number_of_likes() { return $this->hasMany('Like', 'pid', 'pid')->count(); } 

Note:

 This is an API. All will be returned as JSON. 

Update

Return

 Post author_id message Comments(recent 4) user_id message post_date Number_of_likes Likes user_id Number_of_total_comments Number_of_total_likes 

Update

How do I return data

 $posts = $posts->toArray(); $posts = $posts['data']; return Response::json(array( 'data' => $posts )); 

Just using this, I get all the data I want in json. But I also want to add a general score.


Update

 protected $appends = array('total_likes'); public function getTotalLikesAttribute() { return $this->hasMany('Like')->whereUserId($this->uid)->wherePostId($this->pid)->count(); } 

but getting an error:

  Unknown column 'likes.post_id' 

Error

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'likes.post_id' in 'where clause' (SQL: select count(*) as aggregate from `likes` where `likes`.`deleted_at` is null and `likes`.`post_id` = 4 and `pid` = 4 and `uid` = 1) 
+6
source share
4 answers

In your model, place the following accessors:

Total number of votes:

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

Number of comments:

From your description, I see you get the number of posts in the form of comments

 public function getTotalCommentsAttribute() { return $this->hasMany('Post')->whereUserId($this->author_id)->count(); } 

Now, from your controller:

 $post = Jumpsite::find($jid); // total comments var_dump( $post->total_comments ); // total Likes var_dump( $post->total_likes ); 
+7
source

Number of comments for all blog posts in laravel

Step 1: Put this code inside your Post model.

 // Get the comments for the blog post. public function comments() { return $this->hasMany('App\Comment'); } 

Step 2: Put this code inside your PostController.

  $posts= Post::all(); return view('home')->with('posts', $posts); 

Step 3: Then count the comments for all posts using the code below.

 @foreach($posts as $row) {{$row->comments->count()}} @endforeach 

For more details see: http://www.pranms.com/count-comments-for-all-blog-posts-in-laravel/

+2
source

Just one change to get counting results

In a relationship

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

In view

 $post->getTotalLikesAttribute() 
0
source

You can use this following code to calculate the result of a relationship model.

  $posts = App\Post::withCount('comments')->get(); foreach ($posts as $post) { echo $post->comments_count; } 

And also set a condition with so many

 $posts = Post::withCount(['votes', 'comments' => function ($query) { $query->where('content', 'like', 'foo%'); }])->get(); 
0
source

All Articles