Files
ahrommag/app/Models/CommonWord.php
2025-11-16 12:43:07 +03:30

86 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class CommonWord extends Model
{
use HasFactory;
protected $table = 'commonwords';
protected $guarded = [];
const TYPE_SINGLE = 1;
const TYPE_DOUBLE = 2;
const TYPE_TRIPLE = 3;
const TYPE_QUAD = 4;
const STATUS_ACTIVE = 1;
const STATUS_BLOCKED = 0;
protected $appends = ['article_count', 'is_blocked'];
public function blogs()
{
return $this->belongsToMany(Blog::class, 'common_word_blog')
->using(CommonWordBlog::class)
->withPivot(['count', 'is_linked'])
->withTimestamps()
->withoutGlobalScopes();
}
public function blockedWords()
{
return $this->hasOne(BlockedWord::class, 'word', 'title');
}
public function getArticleCountAttribute()
{
return $this->blogs()->count();
}
public function getIsBlockedAttribute()
{
return $this->status == self::STATUS_BLOCKED;
}
public function getLinkedArticlesCountAttribute()
{
return $this->blogs()
->wherePivot('is_linked', true)
->count();
}
public function scopeSingleWords($query)
{
return $query->where('word_type', self::TYPE_SINGLE);
}
public function scopeDoubleWords($query)
{
return $query->where('word_type', self::TYPE_DOUBLE);
}
public function scopeTripleWords($query)
{
return $query->where('word_type', self::TYPE_TRIPLE);
}
public function scopeQuadWords($query)
{
return $query->where('word_type', self::TYPE_QUAD);
}
public function scopeActive($query)
{
return $query->where('status', self::STATUS_ACTIVE);
}
public function scopeBlocked($query)
{
return $query->where('status', self::STATUS_BLOCKED);
}
}