Laravel作为响应返回重复数据

时间:2018-12-03 07:05:08

标签: json laravel

问题在于,它会重复显示数据。在此向我显示了重复数据多次。我无法获得任何解决方案,我该如何解决。请帮助这是我的代码。预先感谢。

public function index(MoodOfTheDay $request){
    $authUser = JWTAuth::parseToken()->toUser();

    $subcategory_ids = $request->get('subcategory_ids');
    $subcategories = Subcategory::whereIn('id', $subcategory_ids)->get();

    $userFriends = $authUser->friendslist()->get();
    $userFriendIds = $authUser->friendslist()->pluck('id')->toArray();

    foreach ($subcategories as $subcategory) {
        $authUser->addMood($subcategory);
    }

    // $user->notify(new FriendInSameMood($authUser, $subcategory));

    $categories = Category::with(['subcategories' => function ($query) use ($subcategory_ids) {
                $query->whereIn('id', $subcategory_ids);
            }])->get();

    $friendsWithMood = [];
    $categoryWithFriendsWithMood = [];
    foreach ($categories as $category) {
        $moodUsers = [];
        $moodSubcategories = $category->subcategories;
        if($moodSubcategories->count()) {
            foreach ($moodSubcategories as $moodSubcategory) {
                $moodUser = $moodSubcategory->moodAdded()->whereIn('id', $userFriendIds)->pluck('id')->toArray();
                $moodUsers = array_unique(array_merge($moodUsers, $moodUser));
            }
            $friendsWithMood['users'] = User::whereIn('id' , $moodUsers)->with('userMoods')->get();
            $categoryWithMood = Category::where('id', $category->id)->first();
            $categoryWithFriendsWithMood[] = array_merge($categoryWithMood->toArray(), $friendsWithMood);
        }
    }

    $activityIds = Activity::whereIn('subcategory_id', $subcategory_ids)->pluck('id')->toArray();
    $companyIds = Company::whereIn('subcategory_id', $subcategory_ids)->pluck('id')->toArray();

    $checkedins = [];
    $comments = [];

    foreach ($userFriends as $userFriend) {
        if($userFriend->checkinCompanies()->count()){
            $checkedinsData = $userFriend->checkinCompanies()
                ->whereIn('id', $companyIds)
                ->with(['companyCheckins' => function ($query) use ($userFriend) {
                    $query->where('user_id', $userFriend->id);
                }])->get();
            $checkedin = [];
            foreach ($checkedinsData as $checkedinData) {
                if ($checkedinData->companyCheckins->count()) {
                    $checkedin[] = $checkedinData;
                }
            }

            $checkedins = array_merge($checkedins, $checkedin);
        }

        if($userFriend->comments()->count()){
            $comment = $userFriend->comments()->whereIn('activity_id', $activityIds)->with(['activity'  => function ($query) use ($authUser) {
                    $query->with(['activityLiked', 'comments', 'joins' => function ($query) use ($authUser) {
                        $query->where('user_id', $authUser->id);
                    }]);
                }])->whereHas('user', function ($query) {
                    $query->where('visible_on_feeds', 1);
                })->get();
            $comments = array_merge($comments, $comment->toArray());
        }
    }

    $created = Activity::whereIn('user_id', $userFriendIds)->whereIn('id', $activityIds)
        ->whereHas('user', function ($query) {
            $query->where('visible_on_feeds', 1);
        })->with(['activityLiked', 'comments', 'joins' => function ($query) use ($authUser) {
            $query->where('user_id', $authUser->id);
        }])->get();

    $joined = ActivityInterest::whereIn('user_id', $userFriendIds)->where('status', 1)->whereIn('activity_id', $activityIds)->whereHas('user', function ($query) {
            $query->where('visible_on_feeds', 1);
        })->with(['activity'  => function ($query) use ($authUser) {
            $query->with(['activityLiked', 'comments', 'joins' => function ($query) use ($authUser) {
                $query->where('user_id', $authUser->id);
            }]);
        }])->get();

    $followingCompanyIds = $authUser->followingCompanies()->whereIn('subcategory_id', $subcategory_ids)->pluck('company_id')->toArray();
    $followingCompanies = Activity::whereIn('company_id', $followingCompanyIds)
        ->with(['company', 'activityLiked', 'comments', 'joins' => function ($query) use ($authUser) {
            $query->where('user_id', $authUser->id);
        }])->get();

    $popularCompanyByCheckins = Company::where('city', $authUser->city)->whereIn('id', $companyIds)
        ->withcount(['companyCheckins'])->orderBy('company_checkins_count', 'desc')->get();

    $popularCompanyByFollowers = Company::where('city', $authUser->city)->whereIn('id', $companyIds)
        ->withcount(['companyFollowers'])->orderBy('company_followers_count', 'desc')->get();

    $companywithcityid = Company::where('city', $authUser->city)->pluck('id')->toArray();
    $popular_activities = Activity::whereIn('id', $activityIds)
        ->where(function ($query) use ($companywithcityid, $authUser) {
            $query->whereIn('company_id', $companywithcityid);
            $query->orWhere('activity_address', $authUser->city);
        })->with(['company', 'activityLiked', 'comments', 'joins' => function ($query) use ($authUser) {
            $query->where('user_id', $authUser->id);
        }])->withcount(['comments','activityLiked','joins'])
        ->orderBy('comments_count', 'desc')
        ->orderBy('activity_liked_count', 'desc')
        ->orderBy('joins_count', 'desc')->get();

    $moodofthedayWithoutSort = array_merge(
        $checkedins, $comments,
        $created->toArray(),
        $joined->toArray(),
        $followingCompanies->toArray(),
        $popularCompanyByCheckins->toArray(),
        $popularCompanyByFollowers->toArray(),
        $popular_activities->toArray());
    $moodofthedayWithSort = array_reverse(array_sort($moodofthedayWithoutSort, function ($value) {
      return $value['created_at'];
    }));

    $moodoftheday = $this->pagination($moodofthedayWithSort, 10);

    return response()->json(['friendsmood'=> $categoryWithFriendsWithMood, 'moodoftheday'=> $moodoftheday ], 200);
   }
}

这是我的代码的JSON响应。在此向我显示了重复数据多次。我无法获得任何解决方案,我该如何解决。请帮助我

     {
         "id": 3,
         "company_name": "ows",
         "company_picture": null,
         "open_hours": null,
         "address": "wapda town",
         "latitude": "0.00000000",
         "longitude": "0.00000000",
         "zipcode": "54000",
         "city": "lahore",
         "country": null,
         "subcategory_id": 4,
         "price": 100,
         "age_limit": 25,
         "company_description": "bla bla bla",
         "created_at": "2018-05-09 12:22:47",
         "updated_at": "2018-05-09 12:22:47",
         "company_followers_count": 2,
         "subcategory": {
             "id": 4,
             "category_id": 2,
             "subcategory_name": "event",
             "subcategory_picture": "scvdd",
             "created_at": null,
             "updated_at": null,
             "category": {
                 "id": 2,
                 "category_name": "nightlife",
                 "category_picture": "knk",
                 "category_color": "#992233",
                 "created_at": null,
                 "updated_at": "2018-08-09 11:21:23"
             }
         },
         "company_checkins": [
             {
                 "id": 2,
                 "facebook_id": null,
                 "name": "umar",
                 "surname": "javed",
                 "email": "uj2@gmail.com",
                 "username": null,
                 "date_of_birth": "1993-01-18",
                 "profile_picture": "ProfileImages/1525268853.png",
                 "occupation": null,
                 "gender": null,
                 "city": null,
                 "country": null,
                 "course": null,
                 "university": null,
                 "discription": null,
                 "visible_on_feeds": 1,
                 "visible_on_guestlists": 1,
                 "created_at": "2018-05-02 11:03:57",
                 "updated_at": "2018-05-02 13:47:33",
                 "pivot": {
                     "company_id": 3,
                     "user_id": 2,
                     "created_at": "2018-07-02 13:46:32",
                     "updated_at": "2018-07-02 13:46:32"
                 }
             },
                {
                    "id": 3,
                    "facebook_id": null,
                    "name": "umar",
                    "surname": "javed",
                    "email": "uj3@gmail.com",
                    "username": null,
                    "date_of_birth": "1993-01-18",
                    "profile_picture": null,
                    "occupation": null,
                    "gender": null,
                    "city": null,
                    "country": null,
                    "course": null,
                    "university": null,
                    "discription": null,
                    "visible_on_feeds": 1,
                    "visible_on_guestlists": 1,
                    "created_at": "2018-05-03 10:50:19",
                    "updated_at": "2018-05-03 10:50:19",
                    "pivot": {
                        "company_id": 3,
                        "user_id": 3,
                        "created_at": "2018-09-26 13:47:07",
                        "updated_at": "2018-07-02 13:47:07"
                    }
                }
            ],
            "company_followers": [
                {
                    "id": 1,
                    "facebook_id": null,
                    "name": "umar",
                    "surname": "javed",
                    "email": "uj1@gmail.com",
                    "username": null,
                    "date_of_birth": "1993-01-18",
                    "profile_picture": null,
                    "occupation": null,
                    "gender": null,
                    "city": "lahore",
                    "country": null,
                    "course": null,
                    "university": null,
                    "discription": null,
                    "visible_on_feeds": 1,
                    "visible_on_guestlists": 1,
                    "created_at": "2018-05-02 10:35:03",
                    "updated_at": "2018-11-23 14:01:55",
                    "pivot": {
                        "company_id": 3,
                        "user_id": 1,
                        "created_at": null,
                        "updated_at": null
                    }
                },
                {
                    "id": 3,
                    "facebook_id": null,
                    "name": "umar",
                    "surname": "javed",
                    "email": "uj3@gmail.com",
                    "username": null,
                    "date_of_birth": "1993-01-18",
                    "profile_picture": null,
                    "occupation": null,
                    "gender": null,
                    "city": null,
                    "country": null,
                    "course": null,
                    "university": null,
                    "discription": null,
                    "visible_on_feeds": 1,
                    "visible_on_guestlists": 1,
                    "created_at": "2018-05-03 10:50:19",
                    "updated_at": "2018-05-03 10:50:19",
                    "pivot": {
                        "company_id": 3,
                        "user_id": 3,
                        "created_at": null,
                        "updated_at": null
                    }
                }
            ]
        },
        {
            "id": 3,
            "company_name": "ows",
            "company_picture": null,
            "open_hours": null,
            "address": "wapda town",
            "latitude": "0.00000000",
            "longitude": "0.00000000",
            "zipcode": "54000",
            "city": "lahore",
            "country": null,
            "subcategory_id": 4,
            "price": 100,
            "age_limit": 25,
            "company_description": "bla bla bla",
            "created_at": "2018-05-09 12:22:47",
            "updated_at": "2018-05-09 12:22:47",
            "company_checkins_count": 2,
            "subcategory": {
                "id": 4,
                "category_id": 2,
                "subcategory_name": "event",
                "subcategory_picture": "scvdd",
                "created_at": null,
                "updated_at": null,
                "category": {
                    "id": 2,
                    "category_name": "nightlife",
                    "category_picture": "knk",
                    "category_color": "#992233",
                    "created_at": null,
                    "updated_at": "2018-08-09 11:21:23"
                }
            },
            "company_checkins": [
                {
                    "id": 2,
                    "facebook_id": null,
                    "name": "umar",
                    "surname": "javed",
                    "email": "uj2@gmail.com",
                    "username": null,
                    "date_of_birth": "1993-01-18",
                    "profile_picture": "ProfileImages/1525268853.png",
                    "occupation": null,
                    "gender": null,
                    "city": null,
                    "country": null,
                    "course": null,
                    "university": null,
                    "discription": null,
                    "visible_on_feeds": 1,
                    "visible_on_guestlists": 1,
                    "created_at": "2018-05-02 11:03:57",
                    "updated_at": "2018-05-02 13:47:33",
                    "pivot": {
                        "company_id": 3,
                        "user_id": 2,
                        "created_at": "2018-07-02 13:46:32",
                        "updated_at": "2018-07-02 13:46:32"
                    }
                },
                {
                    "id": 3,
                    "facebook_id": null,
                    "name": "umar",
                    "surname": "javed",
                    "email": "uj3@gmail.com",
                    "username": null,
                    "date_of_birth": "1993-01-18",
                    "profile_picture": null,
                    "occupation": null,
                    "gender": null,
                    "city": null,
                    "country": null,
                    "course": null,
                    "university": null,
                    "discription": null,
                    "visible_on_feeds": 1,
                    "visible_on_guestlists": 1,
                    "created_at": "2018-05-03 10:50:19",
                    "updated_at": "2018-05-03 10:50:19",
                    "pivot": {
                        "company_id": 3,
                        "user_id": 3,
                        "created_at": "2018-09-26 13:47:07",
                        "updated_at": "2018-07-02 13:47:07"
                    }
                }
            ],
            "company_followers": [
                {
                    "id": 1,
                    "facebook_id": null,
                    "name": "umar",
                    "surname": "javed",
                    "email": "uj1@gmail.com",
                    "username": null,
                    "date_of_birth": "1993-01-18",
                    "profile_picture": null,
                    "occupation": null,
                    "gender": null,
                    "city": "lahore",
                    "country": null,
                    "course": null,
                    "university": null,
                    "discription": null,
                    "visible_on_feeds": 1,
                    "visible_on_guestlists": 1,
                    "created_at": "2018-05-02 10:35:03",
                    "updated_at": "2018-11-23 14:01:55",
                    "pivot": {
                        "company_id": 3,
                        "user_id": 1,
                        "created_at": null,
                        "updated_at": null
                    }
                },
                {
                    "id": 3,
                    "facebook_id": null,
                    "name": "umar",
                    "surname": "javed",
                    "email": "uj3@gmail.com",
                    "username": null,
                    "date_of_birth": "1993-01-18",
                    "profile_picture": null,
                    "occupation": null,
                    "gender": null,
                    "city": null,
                    "country": null,
                    "course": null,
                    "university": null,
                    "discription": null,
                    "visible_on_feeds": 1,
                    "visible_on_guestlists": 1,
                    "created_at": "2018-05-03 10:50:19",
                    "updated_at": "2018-05-03 10:50:19",
                    "pivot": {
                        "company_id": 3,
                        "user_id": 3,
                        "created_at": null,
                        "updated_at": null
                    }
                }
            ]
        },

0 个答案:

没有答案