public function accept(Request $request, $user) { $friendShip = FriendShip::where(['from' => $user, 'accepted' => 0])->first(); if (count($friendShip) == 0) { return response()->json(['error' => 1, 'message' => 'Unexpected Error!']); } if ($request->get('accept') == "yes") { $auth = User::find(Auth::user()->id); $userRequest = User::find($user); // handle add friend $keyFriend = array_merge(array_keys(json_decode($auth->friends, true)), [$user]); $valueFriend = array_merge(array_values(json_decode($auth->friends, true)), [0]); $keyFriend2 = array_merge(array_keys(json_decode($userRequest->friends, true)), [$auth->id]); $valueFriend2 = array_merge(array_values(json_decode($userRequest->friends, true)), [0]); // $auth->friends = json_encode(array_combine($keyFriend, $valueFriend)); $auth->save(); $userRequest->friends = json_encode(array_combine($keyFriend2, $valueFriend2)); $userRequest->save(); $friendShip->update(['accepted' => 1]); return response()->json(['error' => 0, 'message' => 'You and ' . $userRequest->name . ' have been friend ']); } $friendShip->delete(); return response()->json(['error' => 0, 'message' => 'Request cancel!']); }
/** * Bootstrap any application services. * * @return void */ public function boot(Guard $auth) { view()->composer('*', function ($view) use($auth) { if ($auth->check()) { $friendRequest = FriendShip::where(['to' => $auth->user()->id, 'accepted' => 0])->orderBy('id', 'DESC')->get(); $view->with('friendRequest', $friendRequest); } }); }
private function hasFriendRequest() { $requests = FriendShip::where(['to' => Auth::user()->id, 'accepted' => 0, 'seen' => 0])->get(); if ($requests->count() <= 0) { return false; } else { foreach ($requests as $request) { $request->update(['seen' => 1]); $user = $request->getUserSend()->first(); $data[] = array_merge($request->toArray(), ['user' => $user->toArray(), 'xhr' => route('accept-request', $user->id), 'avatar_url' => $user->getProfilePictureUrl()]); } $this->friendRequest = $data; return true; } }