I am using withCount() to count the number of articles in a category, everything works well.
The question is how to count and display the number of articles for All categories?
Controller
$categories = BlogCategory::withCount('articles')->get();
blade.php
<div class="blog-filter">
<div class="blog-filter__item active" data-filter="all">All</div>
@foreach($categories as $category)
<div class="blog-filter__item" data-filter=".category_{{$category->id}}" value="{{ $category->title }} ({{ $category->articles_count }})">{{ $category->title }} ({{ $category->articles_count }})</div>
@endforeach
</div>
({{ $category->articles_count }})
responsible for counting articles
CodePudding user response:
you can use count method:
$allArticlesCount=Article::query()->count();
and do not forget to send it to your view as well.