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;
|
||||
}
|
||||
}
|
||||
100
app/Models/Post.php
Normal file
100
app/Models/Post.php
Normal 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;
|
||||
}
|
||||
}
|
||||
49
app/Models/User.php
Normal file
49
app/Models/User.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user