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
|
|
|
} |
91
|
|
|
|