Files
usher-manage-stack/app/Models/DocumentTag.php
2025-11-20 23:21:05 +08:00

51 lines
1.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class DocumentTag extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'color',
'description',
];
/**
* Boot the model
*/
protected static function booted()
{
static::creating(function ($tag) {
if (empty($tag->slug)) {
$tag->slug = Str::slug($tag->name);
}
});
}
/**
* Get the documents that have this tag
*/
public function documents()
{
return $this->belongsToMany(Document::class, 'document_document_tag')
->withTimestamps();
}
/**
* Get count of active documents with this tag
*/
public function activeDocuments()
{
return $this->belongsToMany(Document::class, 'document_document_tag')
->where('status', 'active')
->withTimestamps();
}
}