-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerScienceResource.php
More file actions
174 lines (153 loc) · 4.72 KB
/
ComputerScienceResource.php
File metadata and controls
174 lines (153 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace App\Models;
use App\Events\TagFrequencyChanged;
use App\Observers\ComputerScienceResourceObserver;
use App\Traits\HasComments;
use App\Traits\HasVotes;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Tags\HasTags;
/**
* @property array $topic_tags
* @property array $programming_language_tags
* @property array $general_tags
*/
#[ObservedBy([ComputerScienceResourceObserver::class])]
class ComputerScienceResource extends Model
{
use CascadesDeletes;
use HasComments;
use HasFactory;
use HasTags;
use HasVotes;
use LogsActivity;
use Sluggable;
use SoftDeletes;
protected $cascadeDeletes = [
// Votes
'votes',
'upvoteSummary',
// Comments
'comments',
'commentsCountRelationship',
'edits',
// Reviews
'reviews',
'reviewSummary',
];
protected $table = 'computer_science_resources';
protected $guarded = ['topic_tags', 'programming_language_tags', 'general_tags'];
protected $appends = ['topic_tags', 'programming_language_tags', 'general_tags', 'vote_score', 'user_vote', 'comments_count', 'image_url'];
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logUnguarded();
}
/**
* Return the sluggable configuration array for this model.
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'name',
],
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Attribute to get the image_url
*/
protected function imageUrl(): Attribute
{
return Attribute::make(
get: fn () => $this->image_path ? Storage::disk('public')->url($this->image_path) : null,
);
}
/**
* Get the review summary relationship.
*/
public function reviewSummary(): HasOne
{
return $this->hasOne(ResourceReviewSummary::class);
}
/**
* Get all the reviews.
*/
public function reviews(): HasMany
{
return $this->hasMany(ResourceReview::class);
}
public function edits(): HasMany
{
return $this->hasMany(ResourceEdits::class);
}
/**
* Attribute to get and set platforms as an array
*/
protected function platforms(): Attribute
{
return Attribute::make(
get: fn ($value) => explode(',', $value),
set: fn ($value) => implode(',', $value)
);
}
/**
* Accessor to get topic tags.
*/
protected function topicTags(): Attribute
{
return Attribute::make(
get: fn () => $this->tagsWithType('topics_tags')->pluck('name')->toArray(),
set: function (array $value) {
$old_value = $this->topic_tags;
$this->syncTagsWithType($value, 'topics_tags');
TagFrequencyChanged::dispatch('topics_tags', $old_value, $value);
return null;
}
);
}
/**
* Accessor to get programming language tags.
*/
protected function programmingLanguageTags(): Attribute
{
return Attribute::make(
get: fn () => $this->tagsWithType('programming_languages_tags')->pluck('name')->toArray(),
set: function (array $value) {
$old_value = $this->programming_language_tags;
$this->syncTagsWithType($value, 'programming_languages_tags');
TagFrequencyChanged::dispatch('programming_languages_tags', $old_value, $value);
return null;
}
);
}
/**
* Accessor to get general tags.
*/
protected function generalTags(): Attribute
{
return Attribute::make(
get: fn () => $this->tagsWithType('general_tags')->pluck('name')->toArray(),
set: function (array $value) {
$old_value = $this->general_tags;
$this->syncTagsWithType($value, 'general_tags');
TagFrequencyChanged::dispatch('general_tags', $old_value, $value);
return null;
}
);
}
}