I am trying to create a post counter by category. That is, category x has so many posts.
For this I am trying to use Eloquent. Almost all the posts belong to one category and there is a post that belongs to another... I attach a screenshot
I have this function that is the one that should count and group by categories. It is in the Modelo
of blog
and blog
is related to blog_category
byblog_category_id
/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getPostCounterAttribute()
{
return $this->blogCategory()->orderBy('blog_category')->count();
}
In the view I have this to get the data of the categories and the posts that each one contains:
@foreach($categories as $category)
<li><a href="{{ url('blogs/'.$category->name) }}">{{ trans('web.blog_category_'.$category->name) }}</a><span>{{$category->postCounter}}</span></li>
@endforeach
but it always shows me 1 in all categories and it is not like that. I know I'm not doing it right.
How could I solve this??
Thank you and regards
update
I have made my query in phpmyadmin and this is the result:
SELECT blog_category.name, count(blogs.id)
FROM blogs
INNER join blog_category
on blogs.blog_category_id = blog_category.id
GROUP by (blog_category.name)
In my blog model, as the answers below have said I try that:
public function getPostCounterAttribute()
{
//return $this->blogCategory()->orderBy('blog_category')->count();
return Blog::withCount('blog_category')->get();
}
result:
The totals do not appear in the view, no property associated with the counter is known
update 2
result of $category
for a foreach category
{"id":10,"name":"recipes","created_at":"2021-02-18 13:07:25","updated_at":"2021-02-25 10:37:38","custom_fields":[],"has_media":false,"media":[]}
$category->postCounter -> no muestra nada
content of$categories
[{"id":10,"name":"recipes","created_at":"2021-02-18 13:07:25","updated_at":"2021-02-25 10:37:38","custom_fields":[],"has_media":false,"media":[]},
update 3
my relationship within BLOG
public function blogCategory()
{
return $this->belongsTo(\App\Models\BlogCategory::class, 'blog_category_id', 'id');
}
update 4
in controller
$postCategories = BlogCategory::withCount('blogs')->get();
return view('web.blog')
->with('data', $data)
->with('posts', $posts)
->with('categories', $categories)
->with('popular', $this->popularPostToday())
->with('postCategories', $postCategories[0]);
To be able to count the posts that exist in a category, you can
withCount
basically useIn order to use it, it is necessary to do the modeling(
hasMany->hasOne, etc
), in your case the relationship you show is "one to many" (blog_category can have many blogs), so in your "Blog" model there must be the relationshiphasMany
So in your controller you now call the function that it names:
EDIT: Use
withCount()
in a modelNormally this is used in the controller because there you assign the method and then you get it in your view, now to put it in the model you have to indicate the one
id
of the record that you want to count because otherwise it would get from all the records. To implement it, we add the following in the "BlogCategory" model:Or use the method
loadCount
, to be able to count the posts that exist in a category, which according to the documentation meansSo he would be
getPostCounterAttribute
like this:It should be noted that this method was implemented from Laravel 6 , so you would have to use the previous function, I put it only as extra information
And in the view we get it like this
$category->postCounter
:Reference