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 CanBeVoted. |
18
|
|
|
*/ |
19
|
|
|
trait CanBeVoted |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Check if item is voted by given user. |
23
|
|
|
* |
24
|
|
|
* @param int $user |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
|
|
public function isVotedBy($user, $type = null) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
return Follow::isRelationExists($this, 'voters', $user); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Check if item is upvoted by given user. |
35
|
|
|
* |
36
|
|
|
* @param int $user |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function isUpvotedBy($user) |
41
|
|
|
{ |
42
|
|
|
return Follow::isRelationExists($this, 'upvoters', $user); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Check if item is downvoted by given user. |
47
|
|
|
* |
48
|
|
|
* @param int $user |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function isDownvotedBy($user) |
53
|
|
|
{ |
54
|
|
|
return Follow::isRelationExists($this, 'downvoters', $user); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return voters. |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
61
|
|
|
*/ |
62
|
|
|
public function voters() |
63
|
|
|
{ |
64
|
|
|
return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table')) |
|
|
|
|
65
|
|
|
->wherePivotIn('relation', [Follow::RELATION_UPVOTE, Follow::RELATION_DOWNVOTE]) |
66
|
|
|
->withPivot('followable_type', 'relation', 'created_at'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return upvoters. |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
73
|
|
|
*/ |
74
|
|
|
public function upvoters() |
75
|
|
|
{ |
76
|
|
|
return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table')) |
77
|
|
|
->wherePivot('relation', '=', Follow::RELATION_UPVOTE) |
78
|
|
|
->withPivot('followable_type', 'relation', 'created_at'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return downvoters. |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
85
|
|
|
*/ |
86
|
|
|
public function downvoters() |
87
|
|
|
{ |
88
|
|
|
return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table')) |
89
|
|
|
->wherePivot('relation', '=', Follow::RELATION_DOWNVOTE) |
90
|
|
|
->withPivot('followable_type', 'relation', 'created_at'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.