/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker::create(); $userIds = DB::table('users')->where('id', '!=', 1)->lists('id'); foreach (range(1, 25) as $index) { FriendRequest::create(['user_id' => 1, 'requester_id' => $faker->randomElement($userIds)]); } }
/** * Execute the command. * * @param UserRepository $userRepository * * @return void */ public function handle(UserRepository $userRepository) { $requestedUser = $userRepository->findById($this->requestedId); $requesterUser = Auth::user(); $friendRequest = FriendRequest::prepareFriendRequest($requesterUser->id); $requestedUser->friendRequests()->save($friendRequest); event(new FriendRequestWasSent($requestedUser, $requesterUser)); return true; }
/** * Remove a friend request. * * @param Request $request * * * @return Response */ public function destroy(Request $request) { $validator = Validator::make($request->all(), ['userId' => 'required']); if ($validator->fails()) { return response()->json(['response' => 'failed', 'message' => 'Something went wrong please try again.']); } else { FriendRequest::where('user_id', $this->currentUser->id)->where('requester_id', $request->userId)->delete(); $friendRequestCount = $this->currentUser->friendRequests()->count(); return response()->json(['response' => 'success', 'count' => $friendRequestCount, 'message' => 'friend request removed']); } }
/** * Store a newly created friend * * @param Request $request * * @return Response */ public function store(Request $request, UserRepository $repository) { $validator = Validator::make($request->all(), ['userId' => 'required']); if ($validator->fails()) { return response()->json(['response' => 'failed', 'message' => 'Something went wrong please try again.']); } else { $this->currentUser->createFriendShipWith($request->userId); $repository->findById($request->userId)->createFriendShipWith($this->currentUser->id); FriendRequest::where('user_id', $this->currentUser->id)->where('requester_id', $request->userId)->delete(); $friendRequestCount = $this->currentUser->friendRequests()->count(); return response()->json(['response' => 'success', 'count' => $friendRequestCount, 'message' => 'Friend request accepted.']); } }