more fixes
This commit is contained in:
@@ -4,11 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\StoryLike;
|
||||
use App\Models\StoryView;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Story — editorial content replacing the legacy Interviews module.
|
||||
* Creator Story model.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $slug
|
||||
@@ -16,10 +21,10 @@ use Illuminate\Database\Eloquent\Model;
|
||||
* @property string|null $excerpt
|
||||
* @property string|null $content
|
||||
* @property string|null $cover_image
|
||||
* @property int|null $author_id
|
||||
* @property int|null $creator_id
|
||||
* @property int $views
|
||||
* @property bool $featured
|
||||
* @property string $status draft|published
|
||||
* @property string $status draft|pending_review|published|scheduled|archived|rejected
|
||||
* @property \Carbon\Carbon|null $published_at
|
||||
* @property int|null $legacy_interview_id
|
||||
*/
|
||||
@@ -35,38 +40,81 @@ class Story extends Model
|
||||
'excerpt',
|
||||
'content',
|
||||
'cover_image',
|
||||
'author_id',
|
||||
'creator_id',
|
||||
'story_type',
|
||||
'reading_time',
|
||||
'views',
|
||||
'likes_count',
|
||||
'comments_count',
|
||||
'featured',
|
||||
'status',
|
||||
'published_at',
|
||||
'scheduled_for',
|
||||
'meta_title',
|
||||
'meta_description',
|
||||
'canonical_url',
|
||||
'og_image',
|
||||
'submitted_for_review_at',
|
||||
'reviewed_at',
|
||||
'reviewed_by_id',
|
||||
'rejected_reason',
|
||||
'legacy_interview_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'featured' => 'boolean',
|
||||
'published_at' => 'datetime',
|
||||
'scheduled_for' => 'datetime',
|
||||
'submitted_for_review_at' => 'datetime',
|
||||
'reviewed_at' => 'datetime',
|
||||
'views' => 'integer',
|
||||
'likes_count' => 'integer',
|
||||
'comments_count' => 'integer',
|
||||
'reading_time' => 'integer',
|
||||
];
|
||||
|
||||
// ── Relations ────────────────────────────────────────────────────────
|
||||
|
||||
public function author()
|
||||
public function creator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(StoryAuthor::class, 'author_id');
|
||||
return $this->belongsTo(User::class, 'creator_id');
|
||||
}
|
||||
|
||||
public function tags()
|
||||
// Legacy alias used by older views/controllers.
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsToMany(StoryTag::class, 'stories_tag_relation', 'story_id', 'tag_id');
|
||||
return $this->creator();
|
||||
}
|
||||
|
||||
public function tags(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(StoryTag::class, 'relation_story_tags', 'story_id', 'tag_id');
|
||||
}
|
||||
|
||||
public function storyViews(): HasMany
|
||||
{
|
||||
return $this->hasMany(StoryView::class, 'story_id');
|
||||
}
|
||||
|
||||
public function storyLikes(): HasMany
|
||||
{
|
||||
return $this->hasMany(StoryLike::class, 'story_id');
|
||||
}
|
||||
|
||||
// ── Scopes ───────────────────────────────────────────────────────────
|
||||
|
||||
public function scopePublished($query)
|
||||
{
|
||||
return $query->where('status', 'published')
|
||||
->where(fn ($q) => $q->whereNull('published_at')->orWhere('published_at', '<=', now()));
|
||||
return $query
|
||||
->where(function ($q): void {
|
||||
$q->where('status', 'published')
|
||||
->orWhere(function ($scheduled): void {
|
||||
$scheduled->where('status', 'scheduled')
|
||||
->whereNotNull('published_at')
|
||||
->where('published_at', '<=', now());
|
||||
});
|
||||
})
|
||||
->where(fn ($q) => $q->whereNull('published_at')->orWhere('published_at', '<=', now()));
|
||||
}
|
||||
|
||||
public function scopeFeatured($query)
|
||||
@@ -95,6 +143,10 @@ class Story extends Model
|
||||
*/
|
||||
public function getReadingTimeAttribute(): int
|
||||
{
|
||||
if (! empty($this->attributes['reading_time'])) {
|
||||
return max(1, (int) $this->attributes['reading_time']);
|
||||
}
|
||||
|
||||
$wordCount = str_word_count(strip_tags((string) $this->content));
|
||||
|
||||
return max(1, (int) ceil($wordCount / 200));
|
||||
|
||||
Reference in New Issue
Block a user