|
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
|
|
|
class Like extends Model |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The attributes that are mass assignable. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array<int, string> |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $fillable = [ |
|
31
|
|
|
'user_id', |
|
32
|
|
|
'model_id', |
|
33
|
|
|
'model_type', |
|
34
|
|
|
'type', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The attributes that should be cast. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array<string, string> |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $casts = [ |
|
43
|
|
|
'model_type' => 'string', |
|
44
|
|
|
'type' => InteractionTypeEnum::class, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check if the record is liked. |
|
49
|
|
|
* |
|
50
|
|
|
* @see InteractionRelationship::likeOne() |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isLiked(): bool |
|
55
|
|
|
{ |
|
56
|
|
|
// Use with likeOne() relationship. Can't use with likes() relationship. |
|
57
|
|
|
return $this->type->isLike(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Check if the record is disliked. |
|
62
|
|
|
* |
|
63
|
|
|
* @see InteractionRelationship::likeOne() |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
public function isDisliked(): bool |
|
68
|
|
|
{ |
|
69
|
|
|
// Use with likeOne() relationship. Can't use with likes() relationship. |
|
70
|
|
|
return $this->type->isDislike(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check if the record is loved. |
|
75
|
|
|
* |
|
76
|
|
|
* @see InteractionRelationship::likeOne() |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public function isLove(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
// Use with likeOne() relationship. Can't use with likes() relationship. |
|
83
|
|
|
return $this->type->isLove(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Scope a query to only include records of a given model type. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Builder $query |
|
90
|
|
|
* @param string $modelType |
|
91
|
|
|
* |
|
92
|
|
|
* @return Builder |
|
93
|
|
|
*/ |
|
94
|
|
|
public function scopeWithModelType(Builder $query, string $modelType): Builder |
|
95
|
|
|
{ |
|
96
|
|
|
// Use with likes() relationship. Can't use with likeOne() relationship. |
|
97
|
|
|
return $query->where('model_type', app($modelType)->getMorphClass()); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|