I want to order my products like this, get all products, group them by color, and get first three color with most item.
$products = Products::select('color', DB::raw('count(*) as total'))
->groupBy('color')
->orderBy('total', 'desc')
->limit(3)
->get();
but it only gives me something like this:
[
{
"color": "red",
"total": 3
},
{
"color": "blue",
"total": 2
},
{
"color": "yellow",
"total": 1
}
]
with no total, It gives me all rows grouped by color, but I want to get all rows grouped by color and order them with total.
something like this order by each total: (red color comes, then blue and yellow)
{
"red": [
{
"id": 1,
"title": "first red product",
"color": "red",
"created_at": "2022-09-03T23:44:59.000000Z",
"updated_at": "2022-09-03T23:44:59.000000Z"
},
{
"id": 3,
"title": "second red product",
"color": "red",
"created_at": "2022-09-03T23:45:40.000000Z",
"updated_at": "2022-09-03T23:45:40.000000Z"
},
{
"id": 6,
"title": "some red product",
"color": "red",
"created_at": "2022-09-03T23:44:59.000000Z",
"updated_at": "2022-09-03T23:44:59.000000Z"
}
],
"blue": [
{
"id": 2,
"title": "some blue product",
"color": "blue",
"created_at": "2022-09-03T23:45:40.000000Z",
"updated_at": "2022-09-03T23:45:40.000000Z"
},
{
"id": 4,
"title": "another blue product",
"color": "blue",
"created_at": "2022-09-03T23:45:40.000000Z",
"updated_at": "2022-09-03T23:45:40.000000Z"
}
],
"yellow": [
{
"id": 5,
"title": "some yellow product",
"color": "yellow",
"created_at": "2022-09-03T23:45:40.000000Z",
"updated_at": "2022-09-03T23:45:40.000000Z"
}
]
}
You can use collection method:groupBy
sortBy
count
map
take
all
$products = Products::all()
->groupBy('color')
->sortBy(fn ($groupValue, $groupKey) => count($groupValue), SORT_REGULAR, true)
->map(fn($groupValue, $groupKey) => $groupValue->take(3))
->all();
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.