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