You need to add visits to your categories table
$mostPopularCategories = Category::orderByDesc('visits')->limit(4)->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How to Query popular categories using the eloquent model. I have a category model and showing popular categories on view. Do i need to created most_viewed or something like that column inside my model ?
note: I want to show only 4 categories. Ask for code if you need Thankyou :)
You need to add visits to your categories table
$mostPopularCategories = Category::orderByDesc('visits')->limit(4)->get();
Please or to participate in this conversation.