51 lines
1.0 KiB
PHP
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();
|
|
}
|
|
}
|