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