From e98bb422b87062d8096c8fdcb672fcb79a4a4b87 Mon Sep 17 00:00:00 2001 From: Nikolay Nesov Date: Tue, 11 Oct 2016 14:05:22 +0000 Subject: [PATCH 1/6] Add method to get groups for a specific friend --- README.md | 5 +++++ src/Traits/Friendable.php | 24 ++++++++++++++++++++++++ tests/FriendshipsGroupsTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/README.md b/README.md index bcf45ff..92529c4 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,11 @@ $user->ungroupFriend($friend, 'family'); $user->ungroupFriend($friend); ``` +#### Get groups for specific friend +```php +$user->getGroupsFor($friend); +``` + #### Get the number of Friends in specific group ```php $user->getFriendsCount($group_name); diff --git a/src/Traits/Friendable.php b/src/Traits/Friendable.php index 789f4d1..c6ea5bd 100644 --- a/src/Traits/Friendable.php +++ b/src/Traits/Friendable.php @@ -353,6 +353,30 @@ public function getFriendsCount($groupSlug = '') return $friendsCount; } + + /** + * Get groups for Friend + * + * @param Model $model + * @return array + */ + public function getGroupsFor(Model $model) { + + $result = []; + $friendship = $this->getFriendship($model); + if(!empty($friendship)) { + $groups = $friendship->groups() + ->where('friend_id', $model->getKey()) + ->where('friend_type', $model->getMorphClass()) + ->get(); + if(false === $groups->isEmpty()) + $result = $groups->pluck('group_slug')->all(); + } + + return $result; + + } + /** * @param Model $recipient * diff --git a/tests/FriendshipsGroupsTest.php b/tests/FriendshipsGroupsTest.php index b41f12a..26658be 100644 --- a/tests/FriendshipsGroupsTest.php +++ b/tests/FriendshipsGroupsTest.php @@ -241,5 +241,35 @@ public function it_returns_user_friends_by_group_per_page() $this->containsOnlyInstancesOf(\App\User::class, $sender->getFriends(0, 'acquaintances')); } + /** @test */ + public function returns_groups_for_specific_friend() { + + $sender = createUser(); + $recipients = createUser([], 2); + $groupsAvailable = config('friendships.groups', []); + + foreach ($recipients as $recipient) { + $sender->befriend($recipient); + $recipient->acceptFriendRequest($sender); + } + + $sender->groupFriend($recipients[0], 'acquaintances'); + $sender->groupFriend($recipients[0], 'close_friends'); + + $sender->groupFriend($recipients[1], 'family'); + + $recipients[0]->groupFriend($sender, 'close_friends'); + + $rec1 = $sender->getGroupsFor($recipients[1]); + + $this->assertCount(2, $sender->getGroupsFor($recipients[0])); + $this->assertCount(1, $recipients[0]->getGroupsFor($sender)); + $this->assertCount(0, $recipients[1]->getGroupsFor($sender)); + + $this->assertInternalType('array', $rec1); + $this->assertContains($rec1[0], $groupsAvailable); + + } + } \ No newline at end of file From 86f725c2c02f1400731182cf612e3aced1378a4e Mon Sep 17 00:00:00 2001 From: Nikolay Nesov Date: Mon, 10 Oct 2016 14:10:33 +0000 Subject: [PATCH 2/6] Update groups config structure --- src/Models/FriendFriendshipGroups.php | 3 +- src/Models/Friendship.php | 13 ++-- src/Traits/Friendable.php | 76 ++++++++++--------- src/config/friendships.php | 6 +- .../create_friendships_groups_table.php | 4 +- tests/FriendshipsGroupsTest.php | 2 - tests/config/friendships.php | 6 +- 7 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/Models/FriendFriendshipGroups.php b/src/Models/FriendFriendshipGroups.php index 807134b..e627450 100644 --- a/src/Models/FriendFriendshipGroups.php +++ b/src/Models/FriendFriendshipGroups.php @@ -2,7 +2,6 @@ namespace Hootlex\Friendships\Models; -use Hootlex\Friendships\Status; use Illuminate\Database\Eloquent\Model; /** @@ -15,7 +14,7 @@ class FriendFriendshipGroups extends Model /** * @var array */ - protected $fillable = ['friendship_id', 'group_id', 'friend_id', 'friend_type']; + protected $fillable = ['friendship_id', 'group_slug', 'friend_id', 'friend_type']; /** * @var bool diff --git a/src/Models/Friendship.php b/src/Models/Friendship.php index b7c8773..f0d19f1 100644 --- a/src/Models/Friendship.php +++ b/src/Models/Friendship.php @@ -2,7 +2,6 @@ namespace Hootlex\Friendships\Models; -use Hootlex\Friendships\Status; use Illuminate\Database\Eloquent\Model; /** @@ -87,23 +86,21 @@ public function scopeWhereSender($query, $model) /** * @param $query * @param Model $model - * @param string $groupSlug + * @param string $group * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeWhereGroup($query, $model, $groupSlug) + public function scopeWhereGroup($query, $model, $group) { $groupsPivotTable = config('friendships.tables.fr_groups_pivot'); $friendsPivotTable = config('friendships.tables.fr_pivot'); $groupsAvailable = config('friendships.groups', []); - if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) { + if ('' !== $group && isset($groupsAvailable[$group])) { - $groupId = $groupsAvailable[$groupSlug]; - - $query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $groupId, $model) { + $query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $group, $model) { $join->on($groupsPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id') - ->where($groupsPivotTable . '.group_id', '=', $groupId) + ->where($groupsPivotTable . '.group_slug', '=', $group) ->where(function ($query) use ($groupsPivotTable, $friendsPivotTable, $model) { $query->where($groupsPivotTable . '.friend_id', '!=', $model->getKey()) ->where($groupsPivotTable . '.friend_type', '=', $model->getMorphClass()); diff --git a/src/Traits/Friendable.php b/src/Traits/Friendable.php index c6ea5bd..71c80d3 100644 --- a/src/Traits/Friendable.php +++ b/src/Traits/Friendable.php @@ -111,22 +111,22 @@ public function denyFriendRequest(Model $recipient) /** * @param Model $friend - * @param $groupSlug + * @param string $group * @return bool */ - public function groupFriend(Model $friend, $groupSlug) + public function groupFriend(Model $friend, $group) { $friendship = $this->findFriendship($friend)->whereStatus(Status::ACCEPTED)->first(); - $groupsAvailable = config('friendships.groups', []); + $groupsAvailable = config('friendships.groups', []); - if (!isset($groupsAvailable[$groupSlug]) || empty($friendship)) { - return false; + if (!isset($groupsAvailable[$group]) || empty($friendship)) { + return false; } $group = $friendship->groups()->firstOrCreate([ 'friendship_id' => $friendship->id, - 'group_id' => $groupsAvailable[$groupSlug], + 'group_slug' => $group, 'friend_id' => $friend->getKey(), 'friend_type' => $friend->getMorphClass(), ]); @@ -137,10 +137,10 @@ public function groupFriend(Model $friend, $groupSlug) /** * @param Model $friend - * @param $groupSlug + * @param string $group * @return bool */ - public function ungroupFriend(Model $friend, $groupSlug = '') + public function ungroupFriend(Model $friend, $group="") { $friendship = $this->findFriendship($friend)->first(); @@ -156,8 +156,8 @@ public function ungroupFriend(Model $friend, $groupSlug = '') 'friend_type' => $friend->getMorphClass(), ]; - if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) { - $where['group_id'] = $groupsAvailable[$groupSlug]; + if ('' !== $group && isset($groupsAvailable[$group])) { + $where['group_slug'] = $group; } $result = $friendship->groups()->where($where)->delete(); @@ -213,34 +213,34 @@ public function getFriendship(Model $recipient) /** * @return \Illuminate\Database\Eloquent\Collection * - * @param string $groupSlug + * @param string $group * */ - public function getAllFriendships($groupSlug = '') + public function getAllFriendships($group = '') { - return $this->findFriendships(null, $groupSlug)->get(); + return $this->findFriendships(null, $group)->get(); } /** * @return \Illuminate\Database\Eloquent\Collection * - * @param string $groupSlug + * @param string $group * */ - public function getPendingFriendships($groupSlug = '') + public function getPendingFriendships($group = '') { - return $this->findFriendships(Status::PENDING, $groupSlug)->get(); + return $this->findFriendships(Status::PENDING, $group)->get(); } /** * @return \Illuminate\Database\Eloquent\Collection * - * @param string $groupSlug + * @param string $group * */ - public function getAcceptedFriendships($groupSlug = '') + public function getAcceptedFriendships($group = '') { - return $this->findFriendships(Status::ACCEPTED, $groupSlug)->get(); + return $this->findFriendships(Status::ACCEPTED, $group)->get(); } /** @@ -294,13 +294,13 @@ public function getFriendRequests() * It will return the 'friends' models. ex: App\User * * @param int $perPage Number - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Collection */ - public function getFriends($perPage = 0, $groupSlug = '') + public function getFriends($perPage = 0, $group = '') { - return $this->getOrPaginate($this->getFriendsQueryBuilder($groupSlug), $perPage); + return $this->getOrPaginate($this->getFriendsQueryBuilder($group), $perPage); } /** @@ -343,13 +343,13 @@ public function getFriendsOfFriends($perPage = 0) /** * Get the number of friends * - * @param string $groupSlug + * @param string $group * * @return integer */ - public function getFriendsCount($groupSlug = '') + public function getFriendsCount($group = '') { - $friendsCount = $this->findFriendships(Status::ACCEPTED, $groupSlug)->count(); + $friendsCount = $this->findFriendships(Status::ACCEPTED, $group)->count(); return $friendsCount; } @@ -413,11 +413,11 @@ private function findFriendship(Model $recipient) /** * @param $status - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Collection */ - private function findFriendships($status = null, $groupSlug = '') + private function findFriendships($status = null, $group = '') { $query = Friendship::where(function ($query) { @@ -426,7 +426,7 @@ private function findFriendships($status = null, $groupSlug = '') })->orWhere(function ($q) { $q->whereRecipient($this); }); - })->whereGroup($this, $groupSlug); + })->whereGroup($this, $group); //if $status is passed, add where clause if (!is_null($status)) { @@ -439,14 +439,14 @@ private function findFriendships($status = null, $groupSlug = '') /** * Get the query builder of the 'friend' model * - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Builder */ - private function getFriendsQueryBuilder($groupSlug = '') + private function getFriendsQueryBuilder($group = '') { - $friendships = $this->findFriendships(Status::ACCEPTED, $groupSlug)->get(['sender_id', 'recipient_id']); + $friendships = $this->findFriendships(Status::ACCEPTED, $group)->get(['sender_id', 'recipient_id']); $recipients = $friendships->pluck('recipient_id')->all(); $senders = $friendships->pluck('sender_id')->all(); @@ -481,11 +481,11 @@ private function getMutualFriendsQueryBuilder(Model $other) /** * Get the query builder for friendsOfFriends ('friend' model) * - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Builder */ - private function friendsOfFriendsQueryBuilder($groupSlug = '') + private function friendsOfFriendsQueryBuilder($group = '') { $friendships = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']); $recipients = $friendships->pluck('recipient_id')->all(); @@ -502,7 +502,7 @@ private function friendsOfFriendsQueryBuilder($groupSlug = '') $q->whereIn('recipient_id', $friendIds); }); }) - ->whereGroup($this, $groupSlug) + ->whereGroup($this, $group) ->get(['sender_id', 'recipient_id']); $fofIds = array_unique( @@ -535,7 +535,12 @@ public function groups() { return $this->morphMany(FriendFriendshipGroups::class, 'friend'); } - + + /** + * @param $builder + * @param $perPage + * @return mixed + */ protected function getOrPaginate($builder, $perPage) { if ($perPage == 0) { @@ -543,4 +548,5 @@ protected function getOrPaginate($builder, $perPage) } return $builder->paginate($perPage); } + } diff --git a/src/config/friendships.php b/src/config/friendships.php index 19ff01f..6147cb3 100644 --- a/src/config/friendships.php +++ b/src/config/friendships.php @@ -8,9 +8,9 @@ ], 'groups' => [ - 'acquaintances' => 0, - 'close_friends' => 1, - 'family' => 2 + 'acquaintances' => 'Acquaintances', + 'close_friends' => 'Close Friends', + 'family' => 'Family' ] ]; \ No newline at end of file diff --git a/src/database/migrations/create_friendships_groups_table.php b/src/database/migrations/create_friendships_groups_table.php index 0f96440..e89193f 100644 --- a/src/database/migrations/create_friendships_groups_table.php +++ b/src/database/migrations/create_friendships_groups_table.php @@ -15,14 +15,14 @@ public function up() { $table->integer('friendship_id')->unsigned(); $table->morphs('friend'); - $table->integer('group_id')->unsigned(); + $table->string('group_slug'); $table->foreign('friendship_id') ->references('id') ->on(config('friendships.tables.fr_pivot')) ->onDelete('cascade'); - $table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_id'], 'unique'); + $table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_slug'], 'unique'); }); diff --git a/tests/FriendshipsGroupsTest.php b/tests/FriendshipsGroupsTest.php index 26658be..1189f8d 100644 --- a/tests/FriendshipsGroupsTest.php +++ b/tests/FriendshipsGroupsTest.php @@ -246,7 +246,6 @@ public function returns_groups_for_specific_friend() { $sender = createUser(); $recipients = createUser([], 2); - $groupsAvailable = config('friendships.groups', []); foreach ($recipients as $recipient) { $sender->befriend($recipient); @@ -267,7 +266,6 @@ public function returns_groups_for_specific_friend() { $this->assertCount(0, $recipients[1]->getGroupsFor($sender)); $this->assertInternalType('array', $rec1); - $this->assertContains($rec1[0], $groupsAvailable); } diff --git a/tests/config/friendships.php b/tests/config/friendships.php index 19ff01f..6147cb3 100644 --- a/tests/config/friendships.php +++ b/tests/config/friendships.php @@ -8,9 +8,9 @@ ], 'groups' => [ - 'acquaintances' => 0, - 'close_friends' => 1, - 'family' => 2 + 'acquaintances' => 'Acquaintances', + 'close_friends' => 'Close Friends', + 'family' => 'Family' ] ]; \ No newline at end of file From 90fd044ca9c5d3ba592fbf4ece42f54575ce50ff Mon Sep 17 00:00:00 2001 From: = Date: Thu, 1 Dec 2016 18:22:56 +0200 Subject: [PATCH 3/6] Store Friendship Groups in DB --- .travis.yml | 11 ++-- src/FriendshipsServiceProvider.php | 8 +-- src/Models/FriendFriendshipGroups.php | 34 ------------ src/Models/Friendship.php | 30 +++++------ src/Models/FriendshipGroup.php | 43 +++++++++++++++ src/Models/FriendshipGrouped.php | 52 ++++++++++++++++++ src/Traits/Friendable.php | 54 +++++++++++-------- src/config/friendships.php | 11 ++-- ...hp => create_friendship_grouped_table.php} | 13 +++-- .../create_friendship_groups_table.php | 29 ++++++++++ src/database/seeds/FriendshipGroupsSeeder.php | 36 +++++++++++++ tests/config/friendships.php | 11 ++-- 12 files changed, 231 insertions(+), 101 deletions(-) delete mode 100644 src/Models/FriendFriendshipGroups.php create mode 100644 src/Models/FriendshipGroup.php create mode 100644 src/Models/FriendshipGrouped.php rename src/database/migrations/{create_friendships_groups_table.php => create_friendship_grouped_table.php} (66%) create mode 100644 src/database/migrations/create_friendship_groups_table.php create mode 100644 src/database/seeds/FriendshipGroupsSeeder.php diff --git a/.travis.yml b/.travis.yml index 9280a71..579f6a1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,14 +24,17 @@ install: before_script: - mysql -e 'create database friendships_test;' - - cp src/database/migrations/create_friendships_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053825_create_friendships_table.php - - yes | cp src/database/migrations/create_friendships_groups_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053826_create_friendships_groups_table.php + - cp src/database/migrations/create_friendships_table.php vendor/laravel/laravel/database/migrations/2015_10_10_000000_create_friendships_table.php + - yes | cp src/database/migrations/create_friendship_groups_table.php vendor/laravel/laravel/database/migrations/2015_10_10_000100_create_friendship_groups_table.php + - yes | cp src/database/migrations/create_friendship_grouped_table.php vendor/laravel/laravel/database/migrations/2015_10_10_000200_create_friendship_grouped_table.php + - yes | cp src/database/seeds/FriendshipGroupsSeeder.php vendor/laravel/laravel/database/seeds/FriendshipGroupsSeeder.php - yes | cp tests/config/friendships.php vendor/laravel/laravel/config/friendships.php - yes | cp tests/Stub_User.php vendor/laravel/laravel/app/User.php - - cd vendor/laravel/laravel + - yes | cd vendor/laravel/laravel - composer update --dev --prefer-source --no-interaction - php artisan migrate - - cd - + - yes | php artisan db:seed --class=FriendshipGroupsSeeder + - yes | cd - script: - vendor/bin/phpunit diff --git a/src/FriendshipsServiceProvider.php b/src/FriendshipsServiceProvider.php index a2ac165..1f6b0b9 100644 --- a/src/FriendshipsServiceProvider.php +++ b/src/FriendshipsServiceProvider.php @@ -14,7 +14,7 @@ class FriendshipsServiceProvider extends ServiceProvider public function boot() { - if (class_exists('CreateFriendshipsTable') || class_exists('CreateFriendshipsGroupsTable')) { + if (class_exists('CreateFriendshipsTable')) { return; } @@ -22,8 +22,9 @@ public function boot() $target = database_path('migrations') . '/'; $this->publishes([ - $stub . 'create_friendships_table.php' => $target . date('Y_m_d_His', time()) . '_create_friendships_table.php', - $stub . 'create_friendships_groups_table.php' => $target . date('Y_m_d_His', time() + 1) . '_create_friendships_groups_table.php' + $stub . 'create_friendships_table.php' => $target . date('Y_m_d_His', time()) . '_create_friendships_table.php', + $stub . 'create_friendship_groups_table.php' => $target . date('Y_m_d_His', time() + 1) . '_create_friendship_groups_table.php', + $stub . 'create_friendship_grouped_table.php' => $target . date('Y_m_d_His', time() + 2) . '_create_friendship_grouped_table.php' ], 'migrations'); $this->publishes([ @@ -40,4 +41,5 @@ public function boot() public function register() { } + } diff --git a/src/Models/FriendFriendshipGroups.php b/src/Models/FriendFriendshipGroups.php deleted file mode 100644 index e627450..0000000 --- a/src/Models/FriendFriendshipGroups.php +++ /dev/null @@ -1,34 +0,0 @@ -table = config('friendships.tables.fr_groups_pivot'); - - parent::__construct($attributes); - } - -} diff --git a/src/Models/Friendship.php b/src/Models/Friendship.php index f0d19f1..b76e48c 100644 --- a/src/Models/Friendship.php +++ b/src/Models/Friendship.php @@ -45,8 +45,8 @@ public function recipient() /** * @return \Illuminate\Database\Eloquent\Relations\hasMany */ - public function groups() { - return $this->hasMany(FriendFriendshipGroups::class, 'friendship_id'); + public function grouped() { + return $this->hasMany(FriendshipGrouped::class, 'friendship_id'); } /** @@ -92,23 +92,19 @@ public function scopeWhereSender($query, $model) public function scopeWhereGroup($query, $model, $group) { - $groupsPivotTable = config('friendships.tables.fr_groups_pivot'); - $friendsPivotTable = config('friendships.tables.fr_pivot'); - $groupsAvailable = config('friendships.groups', []); + $groupUserPivotTable = config('friendships.tables.fr_groups_pivot'); + $groupsTable = config('friendships.tables.fr_groups'); + $friendsPivotTable = config('friendships.tables.fr_pivot'); - if ('' !== $group && isset($groupsAvailable[$group])) { - $query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $group, $model) { - $join->on($groupsPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id') - ->where($groupsPivotTable . '.group_slug', '=', $group) - ->where(function ($query) use ($groupsPivotTable, $friendsPivotTable, $model) { - $query->where($groupsPivotTable . '.friend_id', '!=', $model->getKey()) - ->where($groupsPivotTable . '.friend_type', '=', $model->getMorphClass()); - }) - ->orWhere($groupsPivotTable . '.friend_type', '!=', $model->getMorphClass()); - }); - - } + $query->join($groupUserPivotTable, $groupUserPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id') + ->join($groupsTable, $groupsTable . '.id', '=', $groupUserPivotTable . '.group_id') + ->where($groupsTable . '.slug', '=', $group) + ->where(function ($query) use ($groupUserPivotTable, $friendsPivotTable, $model) { + $query->where($groupUserPivotTable . '.friend_id', '!=', $model->getKey()) + ->where($groupUserPivotTable . '.friend_type', '=', $model->getMorphClass()); + }) + ->orWhere($groupUserPivotTable . '.friend_type', '!=', $model->getMorphClass()); return $query; diff --git a/src/Models/FriendshipGroup.php b/src/Models/FriendshipGroup.php new file mode 100644 index 0000000..8a38fcc --- /dev/null +++ b/src/Models/FriendshipGroup.php @@ -0,0 +1,43 @@ +table = config('friendships.tables.fr_groups'); + + parent::__construct($attributes); + } + + /** + * @return mixed + */ + public function grouped() { + + return $this->hasMany('Hootlex\Friendships\Models\FriendshipGrouped', 'group_id'); + + } + +} diff --git a/src/Models/FriendshipGrouped.php b/src/Models/FriendshipGrouped.php new file mode 100644 index 0000000..41d3b3c --- /dev/null +++ b/src/Models/FriendshipGrouped.php @@ -0,0 +1,52 @@ +table = config('friendships.tables.fr_groups_pivot'); + + parent::__construct($attributes); + } + + /** + * @return mixed + */ + public function group() { + + return $this->belongsTo('Hootlex\Friendships\Models\FriendshipGroup', 'group_id'); + + } + + /** + * @return mixed + */ + public function friendship() { + + return $this->belongsTo('Hootlex\Friendships\Models\Friendship', 'friendship_id'); + + } + +} diff --git a/src/Traits/Friendable.php b/src/Traits/Friendable.php index 71c80d3..6046636 100644 --- a/src/Traits/Friendable.php +++ b/src/Traits/Friendable.php @@ -3,7 +3,8 @@ namespace Hootlex\Friendships\Traits; use Hootlex\Friendships\Models\Friendship; -use Hootlex\Friendships\Models\FriendFriendshipGroups; +use Hootlex\Friendships\Models\FriendshipGrouped; +use Hootlex\Friendships\Models\FriendshipGroup; use Hootlex\Friendships\Status; use Illuminate\Database\Eloquent\Model; use Event; @@ -111,40 +112,37 @@ public function denyFriendRequest(Model $recipient) /** * @param Model $friend - * @param string $group + * @param string $groupSlug * @return bool */ - public function groupFriend(Model $friend, $group) + public function groupFriend(Model $friend, $groupSlug) { - $friendship = $this->findFriendship($friend)->whereStatus(Status::ACCEPTED)->first(); - $groupsAvailable = config('friendships.groups', []); + $friendship = $this->findFriendship($friend)->whereStatus(Status::ACCEPTED)->first(); + $group = FriendshipGroup::where('slug', $groupSlug)->first(); - if (!isset($groupsAvailable[$group]) || empty($friendship)) { - return false; - } + if (empty($group)) return false; - $group = $friendship->groups()->firstOrCreate([ + $grouped = $friendship->grouped()->firstOrCreate([ 'friendship_id' => $friendship->id, - 'group_slug' => $group, + 'group_id' => $group->id, 'friend_id' => $friend->getKey(), 'friend_type' => $friend->getMorphClass(), ]); - return $group->wasRecentlyCreated; + return $grouped->wasRecentlyCreated; } /** * @param Model $friend - * @param string $group + * @param string $groupSlug * @return bool */ - public function ungroupFriend(Model $friend, $group="") + public function ungroupFriend(Model $friend, $groupSlug="") { $friendship = $this->findFriendship($friend)->first(); - $groupsAvailable = config('friendships.groups', []); if (empty($friendship)) { return false; @@ -156,8 +154,9 @@ public function ungroupFriend(Model $friend, $group="") 'friend_type' => $friend->getMorphClass(), ]; - if ('' !== $group && isset($groupsAvailable[$group])) { - $where['group_slug'] = $group; + if (!empty($groupSlug)) { + $group = FriendshipGroup::where('slug', $groupSlug)->first(); + if (!empty($group)) $where['group_id'] = $group->id; } $result = $friendship->groups()->where($where)->delete(); @@ -362,15 +361,24 @@ public function getFriendsCount($group = '') */ public function getGroupsFor(Model $model) { - $result = []; + $result = []; $friendship = $this->getFriendship($model); - if(!empty($friendship)) { - $groups = $friendship->groups() + + if( !empty($friendship) ) { + + $grouped = $friendship->grouped() + ->with('group') ->where('friend_id', $model->getKey()) ->where('friend_type', $model->getMorphClass()) ->get(); - if(false === $groups->isEmpty()) - $result = $groups->pluck('group_slug')->all(); + + if( false === $grouped->isEmpty() ) { + + foreach ($grouped as $item) + $result[] = $item->group->slug; + + } + } return $result; @@ -531,9 +539,9 @@ public function friends() /** * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ - public function groups() + public function grouped() { - return $this->morphMany(FriendFriendshipGroups::class, 'friend'); + return $this->morphMany(FriendshipGrouped::class, 'friend'); } /** diff --git a/src/config/friendships.php b/src/config/friendships.php index 6147cb3..e986266 100644 --- a/src/config/friendships.php +++ b/src/config/friendships.php @@ -3,14 +3,9 @@ return [ 'tables' => [ - 'fr_pivot' => 'friendships', - 'fr_groups_pivot' => 'user_friendship_groups' - ], - - 'groups' => [ - 'acquaintances' => 'Acquaintances', - 'close_friends' => 'Close Friends', - 'family' => 'Family' + 'fr_pivot' => 'friendships', + 'fr_groups_pivot' => 'friendship_grouped', + 'fr_groups' => 'friendship_groups' ] ]; \ No newline at end of file diff --git a/src/database/migrations/create_friendships_groups_table.php b/src/database/migrations/create_friendship_grouped_table.php similarity index 66% rename from src/database/migrations/create_friendships_groups_table.php rename to src/database/migrations/create_friendship_grouped_table.php index e89193f..c56a479 100644 --- a/src/database/migrations/create_friendships_groups_table.php +++ b/src/database/migrations/create_friendship_grouped_table.php @@ -4,9 +4,9 @@ use Illuminate\Database\Migrations\Migration; /** - * Class CreateFriendshipsGroupsTable + * Class CreateFriendshipGroupedTable */ -class CreateFriendshipsGroupsTable extends Migration +class CreateFriendshipGroupedTable extends Migration { public function up() { @@ -14,15 +14,20 @@ public function up() { Schema::create(config('friendships.tables.fr_groups_pivot'), function (Blueprint $table) { $table->integer('friendship_id')->unsigned(); + $table->integer('group_id')->unsigned(); $table->morphs('friend'); - $table->string('group_slug'); + + $table->foreign('group_id') + ->references('id') + ->on(config('friendships.tables.fr_groups')) + ->onDelete('cascade'); $table->foreign('friendship_id') ->references('id') ->on(config('friendships.tables.fr_pivot')) ->onDelete('cascade'); - $table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_slug'], 'unique'); + $table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_id'], 'unique'); }); diff --git a/src/database/migrations/create_friendship_groups_table.php b/src/database/migrations/create_friendship_groups_table.php new file mode 100644 index 0000000..024f311 --- /dev/null +++ b/src/database/migrations/create_friendship_groups_table.php @@ -0,0 +1,29 @@ +increments('id'); + $table->string('slug'); + $table->string('name'); + $table->timestamps(); + + }); + + } + + public function down() { + Schema::dropIfExists(config('friendships.tables.fr_groups')); + } + +} \ No newline at end of file diff --git a/src/database/seeds/FriendshipGroupsSeeder.php b/src/database/seeds/FriendshipGroupsSeeder.php new file mode 100644 index 0000000..1414338 --- /dev/null +++ b/src/database/seeds/FriendshipGroupsSeeder.php @@ -0,0 +1,36 @@ +'acquaintances', 'name'=>'Acquaintances'], + ['slug'=>'close_friends', 'name'=>'Close Friends'], + ['slug'=>'family', 'name'=>'Family'] + + ]; + + foreach ($data as $group) { + + $newGroup = new FriendshipGroup(); + $newGroup->slug = $group['slug']; + $newGroup->name = $group['name']; + $newGroup->save(); + + } + + } + +} \ No newline at end of file diff --git a/tests/config/friendships.php b/tests/config/friendships.php index 6147cb3..e986266 100644 --- a/tests/config/friendships.php +++ b/tests/config/friendships.php @@ -3,14 +3,9 @@ return [ 'tables' => [ - 'fr_pivot' => 'friendships', - 'fr_groups_pivot' => 'user_friendship_groups' - ], - - 'groups' => [ - 'acquaintances' => 'Acquaintances', - 'close_friends' => 'Close Friends', - 'family' => 'Family' + 'fr_pivot' => 'friendships', + 'fr_groups_pivot' => 'friendship_grouped', + 'fr_groups' => 'friendship_groups' ] ]; \ No newline at end of file From 9ad3b09213fe7b8eac056dc14154e7d0475e3780 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 1 Dec 2016 18:32:39 +0200 Subject: [PATCH 4/6] Back to real time --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 579f6a1..2991f53 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,9 +24,9 @@ install: before_script: - mysql -e 'create database friendships_test;' - - cp src/database/migrations/create_friendships_table.php vendor/laravel/laravel/database/migrations/2015_10_10_000000_create_friendships_table.php - - yes | cp src/database/migrations/create_friendship_groups_table.php vendor/laravel/laravel/database/migrations/2015_10_10_000100_create_friendship_groups_table.php - - yes | cp src/database/migrations/create_friendship_grouped_table.php vendor/laravel/laravel/database/migrations/2015_10_10_000200_create_friendship_grouped_table.php + - cp src/database/migrations/create_friendships_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053825_create_friendships_table.php + - yes | cp src/database/migrations/create_friendship_groups_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053826_create_friendship_groups_table.php + - yes | cp src/database/migrations/create_friendship_grouped_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053827_create_friendship_grouped_table.php - yes | cp src/database/seeds/FriendshipGroupsSeeder.php vendor/laravel/laravel/database/seeds/FriendshipGroupsSeeder.php - yes | cp tests/config/friendships.php vendor/laravel/laravel/config/friendships.php - yes | cp tests/Stub_User.php vendor/laravel/laravel/app/User.php From 0bacff8d59477812f353fa6c30a68127c654dc35 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 1 Dec 2016 19:01:50 +0200 Subject: [PATCH 5/6] Fix function naming --- src/Traits/Friendable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Friendable.php b/src/Traits/Friendable.php index 6046636..8d774cd 100644 --- a/src/Traits/Friendable.php +++ b/src/Traits/Friendable.php @@ -159,7 +159,7 @@ public function ungroupFriend(Model $friend, $groupSlug="") if (!empty($group)) $where['group_id'] = $group->id; } - $result = $friendship->groups()->where($where)->delete(); + $result = $friendship->grouped()->where($where)->delete(); return $result; From d1538e5487da1229d72cdbf1277b941128248d2a Mon Sep 17 00:00:00 2001 From: = Date: Fri, 2 Dec 2016 19:55:11 +0200 Subject: [PATCH 6/6] Check if group is not empty --- src/Models/Friendship.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Models/Friendship.php b/src/Models/Friendship.php index b76e48c..76d90fc 100644 --- a/src/Models/Friendship.php +++ b/src/Models/Friendship.php @@ -97,14 +97,18 @@ public function scopeWhereGroup($query, $model, $group) $friendsPivotTable = config('friendships.tables.fr_pivot'); - $query->join($groupUserPivotTable, $groupUserPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id') - ->join($groupsTable, $groupsTable . '.id', '=', $groupUserPivotTable . '.group_id') - ->where($groupsTable . '.slug', '=', $group) - ->where(function ($query) use ($groupUserPivotTable, $friendsPivotTable, $model) { - $query->where($groupUserPivotTable . '.friend_id', '!=', $model->getKey()) - ->where($groupUserPivotTable . '.friend_type', '=', $model->getMorphClass()); - }) - ->orWhere($groupUserPivotTable . '.friend_type', '!=', $model->getMorphClass()); + if (!empty($group)) { + + $query->join($groupUserPivotTable, $groupUserPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id') + ->join($groupsTable, $groupsTable . '.id', '=', $groupUserPivotTable . '.group_id') + ->where($groupsTable . '.slug', '=', $group) + ->where(function ($query) use ($groupUserPivotTable, $friendsPivotTable, $model) { + $query->where($groupUserPivotTable . '.friend_id', '!=', $model->getKey()) + ->where($groupUserPivotTable . '.friend_type', '=', $model->getMorphClass()); + }) + ->orWhere($groupUserPivotTable . '.friend_type', '!=', $model->getMorphClass()); + + } return $query;