|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CSlant\LaravelLike\Traits\Like; |
|
4
|
|
|
|
|
5
|
|
|
use CSlant\LaravelLike\Enums\InteractionTypeEnum; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait LikeScopes |
|
12
|
|
|
* |
|
13
|
|
|
* @package CSlant\LaravelLike\Traits\Like |
|
14
|
|
|
* @mixin Model |
|
15
|
|
|
* |
|
16
|
|
|
* @method MorphOne likeOne() |
|
17
|
|
|
* @method MorphMany likes() |
|
18
|
|
|
*/ |
|
19
|
|
|
trait LikeScopes |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The scope locale for select like relationship with the model (one). |
|
23
|
|
|
* |
|
24
|
|
|
* @return MorphOne |
|
25
|
|
|
*/ |
|
26
|
|
|
public function likeTo(): MorphOne |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->likeOne()->where('type', InteractionTypeEnum::LIKE); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The scope locale for select dislike relationship with the model (one). |
|
33
|
|
|
* |
|
34
|
|
|
* @return MorphOne |
|
35
|
|
|
*/ |
|
36
|
|
|
public function dislikeTo(): MorphOne |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->likeOne()->where('type', InteractionTypeEnum::DISLIKE); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The scope locale for select likes relationship (all). |
|
43
|
|
|
* |
|
44
|
|
|
* @return MorphMany |
|
45
|
|
|
*/ |
|
46
|
|
|
public function likesTo(): MorphMany |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->likes()->where('type', InteractionTypeEnum::LIKE); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The scope locale for select dislikes relationship (all). |
|
53
|
|
|
* |
|
54
|
|
|
* @return MorphMany |
|
55
|
|
|
*/ |
|
56
|
|
|
public function dislikesTo(): MorphMany |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->likes()->where('type', InteractionTypeEnum::DISLIKE); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Check if the model has been interacted by the given user. |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $userId |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isLikedBy(int $userId): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->isInteractedBy($userId, InteractionTypeEnum::LIKE); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check if the model has been interacted by the given user. |
|
75
|
|
|
* |
|
76
|
|
|
* @param int $userId |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public function isDislikedBy(int $userId): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->isInteractedBy($userId, InteractionTypeEnum::DISLIKE); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|