fahdshaykh's avatar

How to count all comments.

i want to count all comments of each post but whereHas relation giving less data count of comments. In database i have checked there is 18 records of each post between dates but the problem is two post_id is same that's why giving me 16 count while i want to get 18 record count.

				 $cc = \App\Models\Post::whereHas('Comments', function ($query) use ($staffs_u, $start, $end) {
                    $query->where('internal_inquiry_status_id', 9);
                    $query->where('created_by', $staffs_u->user->id);
                    $query->whereDate('created_at', '>=', $start);
                    $query->whereDate('created_at', '<=', $end);
                });
							dd($cc->count());

Post model:

              public function comments()
		    {
               return $this->hasMany(Comment::class);
                }

Comment model:

             public function Lead()
           {
               return $this->belongsTo(Post::class);
             }
0 likes
1 reply
martinbean's avatar

@fahdshaykh You’re applying many conditions to the relation. That’s obviously going to reduce the count if there are rows that don’t satisfy the conditions added.

Please or to participate in this conversation.