Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Query to get Pending Friend Requests #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ $user->getAllFriendships();
$user->getPendingFriendships();
```

#### Get a list of pending Requests
```php
$user->getPendingRequests();
```

#### Get a list of accepted Friendships
```php
$user->getAcceptedFriendships();
Expand Down
34 changes: 34 additions & 0 deletions src/Traits/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ public function getPendingFriendships($groupSlug = '')
return $this->findFriendships(Status::PENDING, $groupSlug)->get();
}

/**
* @return \Illuminate\Database\Eloquent\Collection|Friendship[]
*
* @param string $groupSlug
*
*/
public function getPendingRequests($groupSlug = '')
{
return $this->findPendingRequests(Status::PENDING, $groupSlug)->get();
}

/**
* @return \Illuminate\Database\Eloquent\Collection|Friendship[]
*
Expand Down Expand Up @@ -422,6 +433,29 @@ private function findFriendships($status = null, $groupSlug = '')
return $query;
}

/**
* @param $status
* @param string $groupSlug
*
* @return \Illuminate\Database\Eloquent\Collection
*/
private function findPendingRequests($status = null, $groupSlug = '')
{

$query = Friendship::where(function ($query) {
$query->where(function ($q) {
$q->whereSender($this);
});
})->whereGroup($this, $groupSlug);

//if $status is passed, add where clause
if (!is_null($status)) {
$query->where('status', $status);
}

return $query;
}

/**
* Get the query builder of the 'friend' model
*
Expand Down