select c1.name, c1.id, (
select count(*)
from categories as c2
where c2.id = c1.id
group by c2.id) as rating
from categories c1;
// Создаем связь, можно методом в модели, можно на лету, как в примере
Category::resolveRelationUsing('categoryForRating', fn($q) => $orderModel->belongsTo(Category::class, 'id', 'id'));
Category::
withCount('categoryForRating as rating')
->get(['name', 'id', 'rating_count as rating']);