54 lines
1.2 KiB
PHP
54 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
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\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Post extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\PostFactory> */
|
|
use HasFactory;
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'title',
|
|
'slug',
|
|
'content',
|
|
'excerpt',
|
|
'status',
|
|
'published_at',
|
|
'author_id',
|
|
'category_id'
|
|
];
|
|
|
|
protected $casts = [
|
|
'published_at' => 'datetime',
|
|
];
|
|
|
|
protected function avgReadingTime(): Attribute
|
|
{
|
|
return new Attribute(
|
|
get: fn ($value) => ceil(str_word_count($this->content) / 100)
|
|
);
|
|
}
|
|
|
|
public function author(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'author_id');
|
|
}
|
|
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function tags(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Tag::class);
|
|
}
|
|
}
|