Skip to content

Commit c6f3b7e

Browse files
committed
Merge.
2 parents f0b1449 + 341a46f commit c6f3b7e

File tree

13 files changed

+252
-31
lines changed

13 files changed

+252
-31
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: php
22

33
php:
4-
- 5.6
54
- 7.0
65
- 7.1
6+
- 7.2
77

88
dist: trusty
99
sudo: false

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
- Support actions:
1919
- Follow
2020
- Like
21+
- Bookmark
2122
- Subscribe
2223
- Favorite
2324
- Vote (Upvote & Downvote)
2425

2526
## Installation
2627

28+
### Required
29+
30+
- PHP 7.0 +
31+
- Laravel 5.5 +
32+
2733
You can install the package using composer
2834

2935
```sh
@@ -76,10 +82,11 @@ use Overtrue\LaravelFollow\Traits\CanLike;
7682
use Overtrue\LaravelFollow\Traits\CanFavorite;
7783
use Overtrue\LaravelFollow\Traits\CanSubscribe;
7884
use Overtrue\LaravelFollow\Traits\CanVote;
85+
use Overtrue\LaravelFollow\Traits\CanBookmark;
7986

8087
class User extends Model
8188
{
82-
use CanFollow, CanLike, CanFavorite, CanSubscribe, CanVote;
89+
use CanFollow, CanBookmark, CanLike, CanFavorite, CanSubscribe, CanVote;
8390
}
8491
```
8592

@@ -89,10 +96,11 @@ Add `CanBeXXX` Trait to target model, such as 'Post' or 'Music' ...:
8996
use Overtrue\LaravelFollow\Traits\CanBeLiked;
9097
use Overtrue\LaravelFollow\Traits\CanBeFavorited;
9198
use Overtrue\LaravelFollow\Traits\CanBeVoted;
99+
use Overtrue\LaravelFollow\Traits\CanBeBookmarked;
92100

93101
class Post extends Model
94102
{
95-
use CanBeLiked, CanBeFavorited, CanBeVoted;
103+
use CanBeLiked, CanBeFavorited, CanBeVoted, CanBeBookmarked;
96104
}
97105
```
98106

@@ -108,6 +116,7 @@ $user->unfollow($targets)
108116
$user->toggleFollow($targets)
109117
$user->followings()->get() // App\User:class
110118
$user->followings(App\Post::class)->get()
119+
$user->areFollowingEachOther($anotherUser);
111120
$user->isFollowing($target)
112121
```
113122

@@ -118,6 +127,26 @@ $object->followers()->get()
118127
$object->isFollowedBy($user)
119128
```
120129

130+
### Bookmark
131+
132+
#### `\Overtrue\LaravelFollow\Traits\CanBookmark`
133+
134+
```php
135+
$user->bookmark($targets)
136+
$user->unbookmark($targets)
137+
$user->toggleBookmark($targets)
138+
$user->hasBookmarked($target)
139+
$user->bookmarks()->get() // App\User:class
140+
$user->bookmarks(App\Post::class)->get()
141+
```
142+
143+
#### `\Overtrue\LaravelFollow\Traits\CanBeBookmarked`
144+
145+
```php
146+
$object->bookmarkers()->get() // or $object->bookmarkers
147+
$object->isBookmarkedBy($user)
148+
```
149+
121150
### Like
122151

123152
#### `\Overtrue\LaravelFollow\Traits\CanLike`

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "overtrue/laravel-follow",
33
"description": "Laravel 5 User Based System",
44
"require": {
5-
"php": ">=5.5.9"
5+
"php": ">=7.0"
66
},
77
"require-dev": {
8-
"laravel/laravel": "5.*",
8+
"laravel/laravel": "~5.5",
99
"phpunit/phpunit": "~5.0",
1010
"mockery/mockery": "1.0.x-dev"
1111
},

src/Events/RelationToggled.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111

1212
namespace Overtrue\LaravelFollow\Events;
13-
use Illuminate\Database\Eloquent\Model;
1413

1514
/**
1615
* Class RelationToggled.
@@ -20,7 +19,9 @@
2019
class RelationToggled extends Event
2120
{
2221
public $results = [];
22+
2323
public $attached = [];
24+
2425
public $detached = [];
2526

2627
/**

src/Follow.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Follow
3434

3535
const RELATION_FOLLOW = 'follow';
3636

37+
const RELATION_BOOKMARK = 'bookmark';
38+
3739
const RELATION_SUBSCRIBE = 'subscribe';
3840

3941
const RELATION_FAVORITE = 'favorite';
@@ -50,6 +52,8 @@ class Follow
5052
'followers' => 'follow',
5153
'favoriters' => 'favorite',
5254
'favorites' => 'favorite',
55+
'bookmarkers' => 'bookmark',
56+
'bookmarks' => 'bookmark',
5357
'subscriptions' => 'subscribe',
5458
'subscribers' => 'subscribe',
5559
'upvotes' => 'upvote',
@@ -72,6 +76,8 @@ public static function isRelationExists(Model $model, $relation, $target, $class
7276

7377
$relationKey = $class ? 'followable_id' : config('follow.users_table_foreign_key', 'user_id');
7478

79+
$relationKey = self::tablePrefixedField($relationKey);
80+
7581
if ($model->relationLoaded($relation)) {
7682
return $model->{$relation}->where($relationKey, head($target->ids))->isNotEmpty();
7783
}
@@ -217,4 +223,14 @@ protected static function getRelationTypeFromRelation(MorphToMany $relation)
217223

218224
return self::RELATION_TYPES[$relation->getRelationName()];
219225
}
226+
227+
/**
228+
* @param string $field
229+
*
230+
* @return string
231+
*/
232+
protected static function tablePrefixedField($field)
233+
{
234+
return \sprintf('%s.%s', config('follow.followable_table'), $field);
235+
}
220236
}

src/FollowServiceProvider.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,20 @@ class FollowServiceProvider extends ServiceProvider
2020
*/
2121
public function boot()
2222
{
23-
$root = dirname(__DIR__);
23+
$this->publishes([
24+
realpath(__DIR__.'/../config/follow.php') => config_path('follow.php'),
25+
], 'config');
2426

25-
if (!file_exists(config_path('follow.php'))) {
26-
$this->publishes([
27-
$root.'/config/follow.php' => config_path('follow.php'),
28-
], 'config');
29-
}
30-
31-
if (!class_exists('CreateLaravelFollowTables')) {
32-
$datePrefix = date('Y_m_d_His');
33-
$this->publishes([
34-
$root.'/database/migrations/create_laravel_follow_tables.php' => database_path("/migrations/{$datePrefix}_create_laravel_follow_tables.php"),
35-
], 'migrations');
36-
}
27+
$this->publishes([
28+
realpath(__DIR__.'/../database/migrations') => database_path('migrations'),
29+
], 'migrations');
3730
}
3831

3932
/**
4033
* Register the service provider.
4134
*/
4235
public function register()
4336
{
44-
$this->mergeConfigFrom(dirname(__DIR__).'/config/follow.php', 'follow');
37+
$this->mergeConfigFrom(realpath(__DIR__.'/../config/follow.php'), 'follow');
4538
}
4639
}

src/Traits/CanBeBookmarked.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-follow
5+
*
6+
* (c) overtrue <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Overtrue\LaravelFollow\Traits;
13+
14+
use Overtrue\LaravelFollow\Follow;
15+
16+
/**
17+
* Trait CanBeBookmarked.
18+
*/
19+
trait CanBeBookmarked
20+
{
21+
/**
22+
* Check if user is bookmarked by given user.
23+
*
24+
* @param int $user
25+
*
26+
* @return bool
27+
*/
28+
public function isBookmarkedBy($user)
29+
{
30+
return Follow::isRelationExists($this, 'bookmarkers', $user);
31+
}
32+
33+
/**
34+
* Return bookmarkers.
35+
*
36+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
37+
*/
38+
public function bookmarkers()
39+
{
40+
return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
41+
->wherePivot('relation', '=', Follow::RELATION_BOOKMARK)
42+
->withPivot('followable_type', 'relation', 'created_at');
43+
}
44+
}

src/Traits/CanBeFollowed.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Overtrue\LaravelFollow\Traits;
1313

14+
use Illuminate\Support\Facades\DB;
1415
use Overtrue\LaravelFollow\Follow;
1516

1617
/**
@@ -37,8 +38,19 @@ public function isFollowedBy($user)
3738
*/
3839
public function followers()
3940
{
41+
$table = config('follow.followable_table');
42+
$class = \get_class($this);
43+
$userTable = config('follow.user_table', 'users');
44+
$foreignKey = config('follow.users_table_foreign_key', 'user_id');
45+
4046
return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
4147
->wherePivot('relation', '=', Follow::RELATION_FOLLOW)
42-
->withPivot('followable_type', 'relation', 'created_at');
48+
->withPivot('followable_type', 'relation', 'created_at')
49+
->addSelect("{$userTable}.*", DB::raw("pivot_followables.{$foreignKey} IS NOT NULL AS pivot_each_other"))
50+
->leftJoin("{$table} as pivot_followables", function ($join) use ($table, $class, $foreignKey) {
51+
$join->on('pivot_followables.followable_type', '=', DB::raw(\addcslashes("'{$class}'", '\\')))
52+
->on('pivot_followables.followable_id', '=', "{$table}.{$foreignKey}")
53+
->on("pivot_followables.{$foreignKey}", '=', "{$table}.followable_id");
54+
});
4355
}
4456
}

src/Traits/CanBookmark.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-follow
5+
*
6+
* (c) overtrue <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Overtrue\LaravelFollow\Traits;
13+
14+
use Overtrue\LaravelFollow\Follow;
15+
16+
/**
17+
* Trait CanBookmark.
18+
*/
19+
trait CanBookmark
20+
{
21+
/**
22+
* Follow an item or items.
23+
*
24+
* @param int|array|\Illuminate\Database\Eloquent\Model $targets
25+
* @param string $class
26+
*
27+
* @return array
28+
*
29+
* @throws \Exception
30+
*/
31+
public function bookmark($targets, $class = __CLASS__)
32+
{
33+
return Follow::attachRelations($this, 'bookmarks', $targets, $class);
34+
}
35+
36+
/**
37+
* Unbookmark an item or items.
38+
*
39+
* @param int|array|\Illuminate\Database\Eloquent\Model $targets
40+
* @param string $class
41+
*
42+
* @return array
43+
*/
44+
public function unbookmark($targets, $class = __CLASS__)
45+
{
46+
return Follow::detachRelations($this, 'bookmarks', $targets, $class);
47+
}
48+
49+
/**
50+
* Toggle bookmark an item or items.
51+
*
52+
* @param int|array|\Illuminate\Database\Eloquent\Model $targets
53+
* @param string $class
54+
*
55+
* @return array
56+
*
57+
* @throws \Exception
58+
*/
59+
public function toggleBookmark($targets, $class = __CLASS__)
60+
{
61+
return Follow::toggleRelations($this, 'bookmarks', $targets, $class);
62+
}
63+
64+
/**
65+
* Check if user is bookmarked given item.
66+
*
67+
* @param int|array|\Illuminate\Database\Eloquent\Model $target
68+
* @param string $class
69+
*
70+
* @return bool
71+
*/
72+
public function hasBookmarked($target, $class = __CLASS__)
73+
{
74+
return Follow::isRelationExists($this, 'bookmarks', $target, $class);
75+
}
76+
77+
/**
78+
* Return item bookmarks.
79+
*
80+
* @param string $class
81+
*
82+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
83+
*/
84+
public function bookmarks($class = __CLASS__)
85+
{
86+
return $this->morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
87+
->wherePivot('relation', '=', Follow::RELATION_BOOKMARK)
88+
->withPivot('followable_type', 'relation', 'created_at');
89+
}
90+
}

0 commit comments

Comments
 (0)