1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coyote; |
4
|
|
|
|
5
|
|
|
use Coyote\Guide\Role; |
6
|
|
|
use Coyote\Guide\Vote; |
7
|
|
|
use Coyote\Models\Asset; |
8
|
|
|
use Coyote\Models\Subscription; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property int $id |
13
|
|
|
* @property User $user |
14
|
|
|
* @property string $title |
15
|
|
|
* @property string $excerpt |
16
|
|
|
* @property string $text |
17
|
|
|
* @property Tag[] $tags |
18
|
|
|
* @property int $user_id |
19
|
|
|
* @property Comment[] $comments |
20
|
|
|
* @property Comment[] $commentsWithChildren |
21
|
|
|
* @property string $slug |
22
|
|
|
* @property int $votes |
23
|
|
|
* @property string $role |
24
|
|
|
* @property Role[] $roles |
25
|
|
|
*/ |
26
|
|
|
class Guide extends Model |
27
|
|
|
{ |
28
|
|
|
use Taggable; |
29
|
|
|
|
30
|
|
|
protected $fillable = ['title', 'excerpt', 'text']; |
31
|
|
|
|
32
|
|
|
public function getSlugAttribute(): string |
33
|
|
|
{ |
34
|
|
|
return str_slug($this->title, '_'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
39
|
|
|
*/ |
40
|
|
|
public function voters() |
41
|
|
|
{ |
42
|
|
|
return $this->hasMany(Vote::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function user() |
46
|
|
|
{ |
47
|
|
|
return $this->belongsTo(User::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function subscribers() |
51
|
|
|
{ |
52
|
|
|
return $this->morphMany(Subscription::class, 'resource'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
57
|
|
|
*/ |
58
|
|
|
public function page() |
59
|
|
|
{ |
60
|
|
|
return $this->morphOne(Page::class, 'content'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
65
|
|
|
*/ |
66
|
|
|
public function tags() |
67
|
|
|
{ |
68
|
|
|
return $this->morphToMany(Tag::class, 'resource', 'tag_resources'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function comments() |
72
|
|
|
{ |
73
|
|
|
return $this->morphMany(Comment::class, 'resource'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function roles() |
77
|
|
|
{ |
78
|
|
|
return $this->hasMany(Role::class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
83
|
|
|
*/ |
84
|
|
|
public function assets() |
85
|
|
|
{ |
86
|
|
|
return $this->morphMany(Asset::class, 'content'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function commentsWithChildren() |
90
|
|
|
{ |
91
|
|
|
$userRelation = fn ($builder) => $builder->select(['id', 'name', 'photo', 'deleted_at', 'is_blocked', 'is_online'])->withTrashed(); |
92
|
|
|
|
93
|
|
|
return $this |
94
|
|
|
->comments() |
95
|
|
|
->whereNull('parent_id') |
96
|
|
|
->orderBy('id', 'DESC') |
97
|
|
|
->with([ |
98
|
|
|
'children' => function ($builder) use ($userRelation) { |
99
|
|
|
return $builder->orderBy('id')->with(['user' => $userRelation]); |
100
|
|
|
}, |
101
|
|
|
'user' => $userRelation |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function loadUserVoterRelation(?int $userId): void |
106
|
|
|
{ |
107
|
|
|
if (!$userId) { |
|
|
|
|
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->load(['voters' => fn ($builder) => $builder->select(['id', 'guide_id', 'user_id'])->where('user_id', $userId)]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function loadUserRoleRelation(?int $userId): void |
115
|
|
|
{ |
116
|
|
|
if (!$userId) { |
|
|
|
|
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->load(['roles' => fn ($builder) => $builder->select(['id', 'guide_id', 'role'])->where('user_id', $userId)]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: