Hey man, i tried doing it like this:
public function showUserLikesData() {
$users = User::all();
$counter = 0;
$countUser = 0;
$countThatCategory = 0;
$categoryName = '';
foreach($users as $user) {
$userLikesData[$countUser]['User Id'] = $user->id;
foreach ($user->likeCategories as $likeCategory) {
$categoryName = $likeCategory->pivot->category_name;
foreach ($user->likeCategories as $likeCategory) {
$checkCategoryName = $likeCategory->pivot->category_name;
if ($categoryName == $checkCategoryName) {
$countThatCategory++;
}
}
$userLikesData[$countUser]['Like Categories'][$counter]['Category'] = $categoryName;
$userLikesData[$countUser]['Like Categories'][$counter]['Amount'] = $countThatCategory;
$countThatCategory = 0;
$counter++;
}
$countUser++;
$counter=0;
}
return $userLikesData;
}
But it returns this output:
[
{
"User Id": 1,
"Like Categories": [
{
"Category": "Chinese Restaurant",
"Amount": 1
},
{
"Category": "Korean Restaurant",
"Amount": 2
},
{
"Category": "Korean Restaurant",
"Amount": 2
},
{
"Category": "Fast Food Restaurant",
"Amount": 3
},
{
"Category": "Fast Food Restaurant",
"Amount": 3
},
{
"Category": "Fast Food Restaurant",
"Amount": 3
},
{
"Category": "Italian Restaurant",
"Amount": 1
},
{
"Category": "Steakhouse",
"Amount": 3
},
{
"Category": "Steakhouse",
"Amount": 3
},
{
"Category": "Steakhouse",
"Amount": 3
}
]
},
{
"User Id": 2,
"Like Categories": [
{
"Category": "Thai Restaurant",
"Amount": 1
},
{
"Category": "Kebab Shop",
"Amount": 3
},
{
"Category": "Kebab Shop",
"Amount": 3
},
{
"Category": "Kebab Shop",
"Amount": 3
},
{
"Category": "Pizza Place",
"Amount": 2
},
{
"Category": "Pizza Place",
"Amount": 2
},
{
"Category": "Steakhouse",
"Amount": 1
}
]
}
]