1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\LaravelLike\Models; |
4
|
|
|
|
5
|
|
|
use CSlant\LaravelLike\Enums\InteractionTypeEnum; |
6
|
|
|
use CSlant\LaravelLike\Traits\InteractionRelationship; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Query\Builder; |
9
|
|
|
use Illuminate\Support\Carbon; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Like |
13
|
|
|
* |
14
|
|
|
* @package CSlant\LaravelLike\Models |
15
|
|
|
* @property int $id |
16
|
|
|
* @property int $user_id |
17
|
|
|
* @property int $model_id |
18
|
|
|
* @property string $model_type |
19
|
|
|
* @property InteractionTypeEnum $type |
20
|
|
|
* @property Carbon $created_at |
21
|
|
|
* @property Carbon $updated_at |
22
|
|
|
* |
23
|
|
|
* @property-read string $interaction_type getInteractionTypeAttribute() |
24
|
|
|
*/ |
25
|
|
|
class Like extends Model |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The attributes that are mass assignable. |
29
|
|
|
* |
30
|
|
|
* @var array<int, string> |
31
|
|
|
*/ |
32
|
|
|
protected $fillable = [ |
33
|
|
|
'user_id', |
34
|
|
|
'model_id', |
35
|
|
|
'model_type', |
36
|
|
|
'type', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The attributes that should be cast. |
41
|
|
|
* |
42
|
|
|
* @var array<string, string> |
43
|
|
|
*/ |
44
|
|
|
protected $casts = [ |
45
|
|
|
'model_type' => 'string', |
46
|
|
|
'type' => InteractionTypeEnum::class, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if the record is liked. |
51
|
|
|
* |
52
|
|
|
* @see InteractionRelationship::likeOne() |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function isLiked(): bool |
57
|
|
|
{ |
58
|
|
|
// Use with likeOne() relationship. Can't use with likes() relationship. |
59
|
|
|
return $this->type->isLike(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if the record is disliked. |
64
|
|
|
* |
65
|
|
|
* @see InteractionRelationship::likeOne() |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function isDisliked(): bool |
70
|
|
|
{ |
71
|
|
|
// Use with likeOne() relationship. Can't use with likes() relationship. |
72
|
|
|
return $this->type->isDislike(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if the record is loved. |
77
|
|
|
* |
78
|
|
|
* @see InteractionRelationship::likeOne() |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isLove(): bool |
83
|
|
|
{ |
84
|
|
|
// Use with likeOne() relationship. Can't use with likes() relationship. |
85
|
|
|
return $this->type->isLove(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Scope a query to only include records of a given model type. |
90
|
|
|
* |
91
|
|
|
* @param Builder $query |
92
|
|
|
* @param string $modelType |
93
|
|
|
* |
94
|
|
|
* @return Builder |
95
|
|
|
*/ |
96
|
|
|
public function scopeWithModelType(Builder $query, string $modelType): Builder |
97
|
|
|
{ |
98
|
|
|
// Use with likes() relationship. Can't use with likeOne() relationship. |
99
|
|
|
return $query->where('model_type', app($modelType)->getMorphClass()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the interaction type attribute. Used for the accessor. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getInteractionTypeAttribute(): string |
108
|
|
|
{ |
109
|
|
|
return $this->type->value; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|