71 lines
1.7 KiB
PHP
71 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Category extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'description',
|
|
];
|
|
|
|
/**
|
|
* Boot the model and register model events.
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Category $category): void {
|
|
if (blank($category->slug)) {
|
|
$category->slug = static::generateUniqueSlug($category->name);
|
|
}
|
|
});
|
|
|
|
static::updating(function (Category $category): void {
|
|
if ($category->isDirty('name') && blank($category->slug)) {
|
|
$category->slug = static::generateUniqueSlug($category->name, $category->id);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the posts associated with the category.
|
|
*/
|
|
public function posts(): HasMany
|
|
{
|
|
return $this->hasMany(Post::class);
|
|
}
|
|
|
|
/**
|
|
* Generate a unique slug based on the category name.
|
|
*/
|
|
public static function generateUniqueSlug(string $name, ?int $ignoreId = null): string
|
|
{
|
|
$baseSlug = Str::slug($name);
|
|
$slug = $baseSlug;
|
|
$counter = 1;
|
|
|
|
while (static::query()
|
|
->where('slug', $slug)
|
|
->when($ignoreId, fn ($query) => $query->whereKeyNot($ignoreId))
|
|
->exists()) {
|
|
$slug = "{$baseSlug}-{$counter}";
|
|
$counter++;
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
}
|