1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Core\Models; |
4
|
|
|
|
5
|
|
|
use AllowDynamicProperties; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use CSlant\Blog\Core\Models\Base\BaseUser; |
8
|
|
|
use CSlant\LaravelLike\Enums\InteractionTypeEnum; |
9
|
|
|
use CSlant\LaravelLike\UserHasInteraction; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Illuminate\Database\Eloquent\Collection; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class User |
16
|
|
|
* |
17
|
|
|
* @package CSlant\Blog\Core\Models |
18
|
|
|
* |
19
|
|
|
* @property int $id |
20
|
|
|
* @property string $name |
21
|
|
|
* @property string $email |
22
|
|
|
* @property string $password |
23
|
|
|
* @property string $remember_token |
24
|
|
|
* @property string $first_name |
25
|
|
|
* @property string $last_name |
26
|
|
|
* @property string $username |
27
|
|
|
* @property object $permissions |
28
|
|
|
* @property int $super_user |
29
|
|
|
* @property Carbon $created_at |
30
|
|
|
* @property string $avatar_url Property for avatar image url |
31
|
|
|
* @property int $posts_count |
32
|
|
|
* @property null|Role $roles |
33
|
|
|
* @property Post[] $posts |
34
|
|
|
* |
35
|
|
|
* @method static Builder|User newModelQuery() |
36
|
|
|
* @method static Builder|User newQuery() |
37
|
|
|
* @method static Builder|User query() |
38
|
|
|
* @method static Builder|User first() |
39
|
|
|
* @method static Builder|User find($id) |
40
|
|
|
* @method static Builder|User whereCreatedAt($value) |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
#[AllowDynamicProperties] |
44
|
|
|
class User extends BaseUser |
45
|
|
|
{ |
46
|
|
|
use UserHasInteraction; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Defining an posts count accessor |
50
|
|
|
* ->posts_count |
51
|
|
|
* |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
public function getPostsCountAttribute(): int |
55
|
|
|
{ |
56
|
|
|
return $this->posts()->count(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function posts(): HasMany |
60
|
|
|
{ |
61
|
|
|
return $this->hasMany(Post::class, 'author_id'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get all posts liked by the user |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
68
|
|
|
*/ |
69
|
|
|
public function likedPosts(): Collection |
70
|
|
|
{ |
71
|
|
|
$likedPostIds = $this->likes() |
72
|
|
|
->where('model_type', Post::class) |
73
|
|
|
->where('type', InteractionTypeEnum::LIKE) |
74
|
|
|
->pluck('model_id') |
75
|
|
|
->toArray(); |
76
|
|
|
|
77
|
|
|
return Post::whereIn('id', $likedPostIds)->get(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|