检查用户是否为好友并仅向搜索中的好友显示活动的简单方法

时间:2018-11-26 15:27:47

标签: php database laravel

我正在尝试检查用户是否是auth用户的朋友,并且如果它搜索活动,则仅显示由其who_activity_privacy_visible字段等于1的朋友发布的活动。这是我的代码,请帮忙。

public function search(SearchRequest $request)
{
    // getting auth user
    $authUser = JWTAuth::parseToken()->toUser();

    // check if there's search params
    if($request->has('search')) {
        $userFriendIds = $authUser->friendslist()->pluck('id')->toArray();
        $user_result = Searchy::search('users')->fields('name', 'username')
            ->query($request->get('search'))
            ->select('id', 'name', 'surname', 'username', 'profile_picture')
            ->getQuery()->whereIn('id', $userFriendIds)
            ->having('relevance', '>', 50)->limit(10)->get();
        $userIds = array_pluck($user_result, 'id');
        $users = User::whereIn('id', $userIds)->with(['checkinCompanies', 'joins'])->get();

        $company_result = Searchy::search('companies')->fields('company_name', 'city')
            ->query($request->get('search'))->getQuery()
            ->having('relevance', '>', 50)->limit(10)->get();
        $companyIds = array_pluck($company_result, 'id');
        $companies = Company::whereIn('id', $companyIds)->with(['subcategory'])->get();

        $activity_result = Searchy::search('activities')
            ->fields('activity_title', 'activity_description', 'activity_address')
            ->query($request->get('search'))->getQuery()
            ->having('relevance', '>', 50)->limit(10)->get();
        $activityIds = array_pluck($activity_result, 'id');
        $activities = Activity::whereIn('id', $activityIds)->with('subcategory','joins')->where(function ($query) use ($user, $authUser){ 
            if ($authUser->isFriendsWith($user)) {
                $query->get();
            }
            })->get();


        if($users->count()) {
            return response()->json(['users' => $users], 200);
        }
        elseif($companies->count() && $activities->count()) {
            return response()->json(['companies' => $companies, 'activities' => $activities], 200);
        }
        elseif($companies->count()) {
            return response()->json(['companies' => $companies], 200);
        }
        elseif($activities->count()) {
            return response()->json(['activities' => $activities], 200);
        }
        else {
            return response()->json(['error' => 'No results found, please try another keyword.'], 200);
        }
    }

    // Return the error message if no keywords existed
    return response()->json(['error' => 'No results found, please enter at least 1 keyword.'], 200);

}

2 个答案:

答案 0 :(得分:0)

我会在用户模型中编写类似getAllFriends()的方法,该方法将返回用户集合,然后使用where()在其中搜索活动。

您是否使用其他软件包?如果是,请告诉我们。

答案 1 :(得分:0)

这是我添加的代码,它可以正常工作

        $activities = [];
        foreach ($activity_result as $activity) {
            if(in_array($activity->user_id, $userFriendIds))
            {
                $activitydetail = Activity::where('id', $activity->id)->with('joins')->get();
            } else {
                $activitydetail = Activity::where('id', $activity->id)
                    ->where('activity_privacy_visible', 0)->with('joins')->get();
            }
            $activities = array_merge($activities, $activitydetail->toArray());
        }