1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Core\Models; |
4
|
|
|
|
5
|
|
|
use AllowDynamicProperties; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use CSlant\Blog\Core\Models\Base\BasePost; |
8
|
|
|
use CSlant\LaravelLike\HasLike; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Post |
14
|
|
|
* |
15
|
|
|
* @package CSlant\Blog\Core\Models |
16
|
|
|
* |
17
|
|
|
* @property int $id |
18
|
|
|
* @property string $name |
19
|
|
|
* @property string $description |
20
|
|
|
* @property string $content |
21
|
|
|
* @property string $status |
22
|
|
|
* @property int $author_id |
23
|
|
|
* @property string $author_type |
24
|
|
|
* @property int $is_featured |
25
|
|
|
* @property int $views |
26
|
|
|
* @property string $format_type |
27
|
|
|
* @property Carbon $created_at |
28
|
|
|
* @property Carbon $updated_at |
29
|
|
|
* @property Slug $slug |
30
|
|
|
* @property string $url |
31
|
|
|
* @property Tag[] $tags |
32
|
|
|
* @property Category[] $categories |
33
|
|
|
* @property string $image |
34
|
|
|
* @property string $author |
35
|
|
|
* |
36
|
|
|
* @method static Builder|Post newModelQuery() |
37
|
|
|
* @method static Builder|Post newQuery() |
38
|
|
|
* @method static Builder|Post query() |
39
|
|
|
* @method static Builder|Post first() |
40
|
|
|
* @method static Builder|Post find($id) |
41
|
|
|
* @method static Builder|Post with($relations) |
42
|
|
|
* @method static Builder|Post whereId($value) |
43
|
|
|
* @method static Builder|Post whereIn($column, $values) |
44
|
|
|
* @method static Builder|Post where($column, $operator = null, $value = null, $boolean = 'and') |
45
|
|
|
* @method static Post findOrFail($id) |
46
|
|
|
* @method static Post create($data) |
47
|
|
|
* |
48
|
|
|
* @mixin BasePost |
49
|
|
|
*/ |
50
|
|
|
#[AllowDynamicProperties] |
51
|
|
|
class Post extends BasePost |
52
|
|
|
{ |
53
|
|
|
use HasLike; |
54
|
|
|
|
55
|
|
|
public function slug(): HasOne |
56
|
|
|
{ |
57
|
|
|
return $this->hasOne(Slug::class, 'reference_id', 'id') |
58
|
|
|
->where('reference_type', $this->getBaseModel()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Like the post by the given user |
63
|
|
|
* |
64
|
|
|
* @param \CSlant\Blog\Core\Models\User $user |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function likeBy($user): bool |
68
|
|
|
{ |
69
|
|
|
$this->likes()->create([ |
70
|
|
|
'user_id' => $user->id, |
71
|
|
|
'type' => \CSlant\LaravelLike\Enums\InteractionTypeEnum::LIKE, |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Unlike the post by the given user |
79
|
|
|
* |
80
|
|
|
* @param \CSlant\Blog\Core\Models\User $user |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function unlikeBy($user): bool |
84
|
|
|
{ |
85
|
|
|
return $this->likes() |
86
|
|
|
->where('user_id', $user->id) |
87
|
|
|
->where('type', \CSlant\LaravelLike\Enums\InteractionTypeEnum::LIKE) |
88
|
|
|
->delete(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if the post is liked by the user |
93
|
|
|
* |
94
|
|
|
* @param \CSlant\Blog\Core\Models\User $user |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isLikedBy($user) |
98
|
|
|
{ |
99
|
|
|
if (!$user) { |
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->likes() |
104
|
|
|
->where('user_id', $user->id) |
105
|
|
|
->exists(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|