Migration complete
This commit is contained in:
@@ -4,12 +4,14 @@ namespace App\Models;
|
||||
|
||||
use App\Helpers\EntryHelpers;
|
||||
use App\Traits\HasGallery;
|
||||
use App\Traits\HasXenforoUserId;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use League\CommonMark\GithubFlavoredMarkdownConverter;
|
||||
use Monolog\Level;
|
||||
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
||||
use Spatie\Activitylog\Support\LogOptions;
|
||||
@@ -97,12 +99,17 @@ use Spatie\Activitylog\Support\LogOptions;
|
||||
* @method static Builder<static>|Entry withoutTrashed()
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Spatie\Activitylog\Models\Activity> $activitiesAsSubject
|
||||
* @property-read int|null $activities_as_subject_count
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\EntryReview> $reviews
|
||||
* @property-read int|null $reviews_count
|
||||
* @property-read float $average_rating
|
||||
* @property-read int $reviews_count_cached
|
||||
* @property-read string $description_html
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class Entry extends Model
|
||||
{
|
||||
|
||||
use SoftDeletes, HasGallery, LogsActivity;
|
||||
use SoftDeletes, HasGallery, LogsActivity, HasXenforoUserId;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
@@ -143,6 +150,15 @@ class Entry extends Model
|
||||
'featured_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected static function booted(): void {
|
||||
static::saving( function( $entry ) {
|
||||
if( $entry->isDirty('version') ) {
|
||||
$entry->created_at = now();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public function scopePublished( Builder $query ): Builder {
|
||||
return $query->where( 'state', 'published' );
|
||||
}
|
||||
@@ -207,6 +223,30 @@ class Entry extends Model
|
||||
return $this->hasMany(EntryHash::class);
|
||||
}
|
||||
|
||||
public function reviews(): HasMany {
|
||||
return $this->hasMany(EntryReview::class);
|
||||
}
|
||||
|
||||
public function getAverageRatingAttribute(): float
|
||||
{
|
||||
return round( $this->reviews->avg('rating') ?? 0, 1 );
|
||||
}
|
||||
|
||||
public function getReviewsCountCachedAttribute(): int
|
||||
{
|
||||
return $this->reviews->count();
|
||||
}
|
||||
|
||||
public function getDescriptionHtmlAttribute(): string
|
||||
{
|
||||
$converter = new GithubFlavoredMarkdownConverter([
|
||||
'html_input' => 'strip',
|
||||
'allow_unsafe_links' => false,
|
||||
]);
|
||||
|
||||
return $converter->convert($this->description)->getContent();
|
||||
}
|
||||
|
||||
public function parseStaffCredits(): ?array {
|
||||
return json_decode( $this->staff_credits ?? "", true );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user