Skip to content

Commit bf9ce8c

Browse files
authored
Merge pull request #53 from Mombuyish/master
Add Bookmark feature
2 parents 93baadf + 10e462e commit bf9ce8c

File tree

4 files changed

+162
-2
lines changed

4 files changed

+162
-2
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Support actions:
1919
- Follow
2020
- Like
21+
- Bookmark
2122
- Subscribe
2223
- Favorite
2324
- Vote (Upvote & Downvote)
@@ -79,7 +80,7 @@ use Overtrue\LaravelFollow\Traits\CanVote;
7980

8081
class User extends Model
8182
{
82-
use CanFollow, CanLike, CanFavorite, CanSubscribe, CanVote;
83+
use CanFollow, CanBookmark, CanLike, CanFavorite, CanSubscribe, CanVote;
8384
}
8485
```
8586

@@ -89,10 +90,11 @@ Add `CanBeXXX` Trait to target model, such as 'Post' or 'Music' ...:
8990
use Overtrue\LaravelFollow\Traits\CanBeLiked;
9091
use Overtrue\LaravelFollow\Traits\CanBeFavorited;
9192
use Overtrue\LaravelFollow\Traits\CanBeVoted;
93+
use Overtrue\LaravelFollow\Traits\CanBookmarked;
9294

9395
class Post extends Model
9496
{
95-
use CanBeLiked, CanBeFavorited, CanBeVoted;
97+
use CanBeLiked, CanBeFavorited, CanBeVoted, CanBeBookmarked;
9698
}
9799
```
98100

@@ -118,6 +120,26 @@ $object->followers()->get()
118120
$object->isFollowedBy($user)
119121
```
120122

123+
### Bookmark
124+
125+
#### `\Overtrue\LaravelFollow\Traits\CanBookmark`
126+
127+
```php
128+
$user->bookmark($targets)
129+
$user->unbookmark($targets)
130+
$user->toggleBookmark($targets)
131+
$user->hasBookmarked($target)
132+
$user->bookmarks()->get() // App\User:class
133+
$user->bookmarks(App\Post::class)->get()
134+
```
135+
136+
#### `\Overtrue\LaravelFollow\Traits\CanBeBookmarked`
137+
138+
```php
139+
$object->bookmarkers()->get() // or $object->bookmarkers
140+
$object->isBookmarkedBy($user)
141+
```
142+
121143
### Like
122144

123145
#### `\Overtrue\LaravelFollow\Traits\CanLike`

src/Follow.php

Lines changed: 4 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',

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/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)