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

100
app/Models/Post.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
class Post extends Model
{
use HasFactory;
public const STATUS_DRAFT = 'draft';
public const STATUS_PUBLISHED = 'published';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'slug',
'content',
'excerpt',
'category_id',
'featured_image',
'is_featured',
'status',
'published_at',
];
/**
* The attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_featured' => 'boolean',
'published_at' => 'datetime',
];
}
/**
* Boot the model and register model events.
*/
protected static function booted(): void
{
static::creating(function (Post $post): void {
if (blank($post->slug)) {
$post->slug = static::generateUniqueSlug($post->title);
}
});
static::updating(function (Post $post): void {
if ($post->isDirty('title') && blank($post->slug)) {
$post->slug = static::generateUniqueSlug($post->title, $post->id);
}
});
}
/**
* Get the category that owns the post.
*/
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
/**
* Generate a unique slug based on the post title.
*/
public static function generateUniqueSlug(string $title, ?int $ignoreId = null): string
{
$baseSlug = Str::slug($title);
$slug = $baseSlug;
$counter = 1;
while (static::query()
->where('slug', $slug)
->when($ignoreId, fn ($query) => $query->whereKeyNot($ignoreId))
->exists()) {
$slug = "{$baseSlug}-{$counter}";
$counter++;
}
return $slug;
}
/**
* Determine whether the post is published.
*/
public function isPublished(): bool
{
return $this->status === self::STATUS_PUBLISHED;
}
}