97 lines
2.4 KiB
PHP
97 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Publication extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'post_id',
|
|
'channel_id',
|
|
'prefix_id',
|
|
'title',
|
|
'excerpt',
|
|
'slug',
|
|
'status',
|
|
'published_at',
|
|
'meta_title',
|
|
'meta_description',
|
|
'og_image',
|
|
'canonical_url',
|
|
];
|
|
|
|
protected $casts = [
|
|
'published_at' => 'datetime',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function (Publication $publication) {
|
|
if (empty($publication->slug) && !empty($publication->title)) {
|
|
$publication->slug = Str::slug($publication->title);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function post(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Post::class);
|
|
}
|
|
|
|
public function channel(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Channel::class);
|
|
}
|
|
|
|
public function prefix(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Prefix::class);
|
|
}
|
|
|
|
public function blocks(): HasMany
|
|
{
|
|
return $this->hasMany(Block::class)->orderBy('order');
|
|
}
|
|
|
|
public function taxonomyTerms(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(TaxonomyTerm::class);
|
|
}
|
|
|
|
public function scopePublished(Builder $query): Builder
|
|
{
|
|
return $query->where('status', 'published');
|
|
}
|
|
|
|
public function scopeForChannel(Builder $query, Channel $channel): Builder
|
|
{
|
|
return $query->where('channel_id', $channel->id);
|
|
}
|
|
|
|
public function scopeForPrefix(Builder $query, Prefix $prefix): Builder
|
|
{
|
|
return $query->where('prefix_id', $prefix->id);
|
|
}
|
|
|
|
public function getUrlAttribute(): string
|
|
{
|
|
$this->loadMissing('prefix');
|
|
$this->channel->loadMissing('prefixes');
|
|
$prefixSlug = $this->prefix?->slug
|
|
?? $this->channel->prefixes->first()?->slug
|
|
?? 'p';
|
|
$identifier = $this->slug ?? $this->post_id;
|
|
|
|
return "/{$prefixSlug}/{$identifier}";
|
|
}
|
|
}
|