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 Illuminate\Support\Facades\DB; |
15
|
|
|
use Overtrue\LaravelFollow\Follow; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait CanFollow. |
19
|
|
|
*/ |
20
|
|
|
trait CanFollow |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Follow an item or items. |
24
|
|
|
* |
25
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $targets |
26
|
|
|
* @param string $class |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
* |
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
|
|
public function follow($targets, $class = __CLASS__) |
33
|
|
|
{ |
34
|
|
|
return Follow::attachRelations($this, 'followings', $targets, $class); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Unfollow an item or items. |
39
|
|
|
* |
40
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $targets |
41
|
|
|
* @param string $class |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function unfollow($targets, $class = __CLASS__) |
46
|
|
|
{ |
47
|
|
|
return Follow::detachRelations($this, 'followings', $targets, $class); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Toggle follow an item or items. |
52
|
|
|
* |
53
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $targets |
54
|
|
|
* @param string $class |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
* |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
|
|
public function toggleFollow($targets, $class = __CLASS__) |
61
|
|
|
{ |
62
|
|
|
return Follow::toggleRelations($this, 'followings', $targets, $class); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check if user is following given item. |
67
|
|
|
* |
68
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $target |
69
|
|
|
* @param string $class |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function isFollowing($target, $class = __CLASS__) |
74
|
|
|
{ |
75
|
|
|
return Follow::isRelationExists($this, 'followings', $target, $class); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check if user and target user is following each other. |
80
|
|
|
* |
81
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $target |
82
|
|
|
* @param string $class |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function areFollowingEachOther($target, $class = __CLASS__) |
87
|
|
|
{ |
88
|
|
|
return Follow::isRelationExists($this, 'followings', $target, $class) && Follow::isRelationExists($target, 'followings', $this, $class); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return item followings. |
93
|
|
|
* |
94
|
|
|
* @param string $class |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
97
|
|
|
*/ |
98
|
|
|
public function followings($class = __CLASS__) |
99
|
|
|
{ |
100
|
|
|
$table = config('follow.followable_table'); |
101
|
|
|
$foreignKey = config('follow.users_table_foreign_key', 'user_id'); |
102
|
|
|
$targetTable = (new $class)->getTable(); |
103
|
|
|
|
104
|
|
|
return $this->morphedByMany($class, config('follow.morph_prefix'), $table) |
|
|
|
|
105
|
|
|
->wherePivot('relation', '=', Follow::RELATION_FOLLOW) |
106
|
|
|
->withPivot('followable_type', 'relation', 'created_at') |
107
|
|
|
->addSelect("{$targetTable}.*", DB::raw("pivot_followables.{$foreignKey} IS NOT NULL AS pivot_each_other")) |
108
|
|
|
->leftJoin("{$table} as pivot_followables", function($join) use ($table, $class, $foreignKey) { |
109
|
|
|
|
110
|
|
|
$join->on('pivot_followables.followable_type', '=', DB::raw(\addcslashes("'{$class}'", '\\'))) |
111
|
|
|
->on('pivot_followables.followable_id', '=', "{$table}.{$foreignKey}") |
112
|
|
|
->on("pivot_followables.{$foreignKey}", '=', "{$table}.followable_id"); |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|