Initial commit: Laravel Blog API

This commit is contained in:
2026-05-28 13:50:28 -03:00
commit 8f0ba684fc
71 changed files with 12646 additions and 0 deletions

70
app/Models/Category.php Normal file
View 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;
}
}