mardi 8 octobre 2019

Laravel - Accessing, Manipulate and Looping Through Pivot Table

I have two tables, users and like_categories which has many-to-many relationship. The pivot table is called like_category_user. After inserting two users data into the db, here is my pivot table look like: https://i.imgur.com/MeeRbiV.png

I want to count the amount for each of the different like category for each user and store it in object array like this:

[
    {
        "User Id": 1,
        "Like Categories": [
            {
                "Category": "Chinese Restaurant"
                "Amount": 1
            },
            {
                "Category": "Korean Restaurant"
                "Amount": 2
            },
            {
                "Category": "Fast Food Restaurant"
                "Amount": 3
            },
            {
                "Category": "Italian Restaurant"
                "Amount": 1
            },
            {
                "Category": "Steakhouse Restaurant"
                "Amount": 3
            }
        ]
    },
    {
        "User Id": 2,
        "Like Categories": [
            {
                "Category": "Thai Restaurant"
                "Amount": 1
            },
            {
                "Category": "Kebab Shop"
                "Amount": 3
            },
            {
                "Category": "Pizza Place"
                "Amount": 2
            },
            {
                "Category": "Steakhouse"
                "Amount": 1
            }
        }
    }
]

I have tried doing it but my output is:

[
    {
        "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
            }
        ]
    }
]

I dont which part im doing it wrong but pls correct my code:

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;
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire