45 lines
941 B
PHP
45 lines
941 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Tag extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'color',
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($tag) {
|
|
if (empty($tag->slug)) {
|
|
$tag->slug = Str::slug($tag->name);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function posts(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Post::class)->withTimestamps();
|
|
}
|
|
|
|
public function publishedPosts(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Post::class)
|
|
->withTimestamps()
|
|
->where('status', 'published')
|
|
->where('published_at', '<=', now());
|
|
}
|
|
}
|
|
|