Initial commit
This commit is contained in:
50
app/Models/DocumentTag.php
Normal file
50
app/Models/DocumentTag.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user