posts
id: bigint, unsigned, auto-increment, primary key
user_id: bigint, unsigned, foreign key to users.id
title: string
slug: string, unique
content: text
excerpt: text, nullable
status: enum('draft', 'published', 'archived')
published_at: timestamp, nullable
seo_title: string, nullable
seo_description: text, nullable
created_at: timestamp, nullable
updated_at: timestamp, nullable
deleted_at: timestamp, nullable
Relationships
Belongs to User
Belongs to many Categories
Belongs to many Tags
Has many Likes
Has many Comments
Has many Media
For each of the database table schema above, Using the Chain of Thought Prompting, Meticulously generate a well-structured Laravel migration, model and a model factory class that Adhere to Laravel best practices, including naming conventions, code organization, and SOLID principles and Ensure that the code is clean, maintainable, and follows the DRY (Don't Repeat Yourself) principle and also PHPDoc should be comprehensive, detailing parameters, return types, and exceptions thrown. The aim of the generated code is to adhere to Laravel best practices, coding standard, and deliver the highest standards of software development, delivering a well-documented, robust, and efficient module that fulfills the application's business requirements and provides an exceptional user experience:
Certainly! Below is a comprehensive set of Laravel migrations, models, and factory classes for each module you've specified. The code adheres to Laravel best practices, including naming conventions, code organization, SOLID principles, and DRY (Don't Repeat Yourself) principles. Each class includes comprehensive PHPDoc comments for clarity and maintainability.
Module: Users
Migration: create_users_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateUsersTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Model: User.php
php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
/**
* User Model
*
* Represents a user in the system.
*
* @package App\Models
*
* @property bigint $id
* @property string $name
* @property string $email
* @property string $password
* @property timestamp|null $email_verified_at
* @property string|null $remember_token
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class User extends Authenticatable
{
use HasFactory, Notifiable, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'email_verified_at',
'remember_token',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The roles that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
/**
* Get the posts for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function posts()
{
return $this->hasMany(Post::class);
}
/**
* Get the comments for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
/**
* Get the likes for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function likes()
{
return $this->hasMany(Like::class);
}
}
Factory: UserFactory.php
php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
/**
* User Factory
*
* @package Database\Factories
*/
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\User::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => $this->faker->optional()->dateTime(),
'password' => Hash::make('password'), // default password
'remember_token' => Str::random(10),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Module: Categories
Migration: create_categories_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateCategoriesTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug')->unique();
$table->text('description')->nullable();
$table->unsignedBigInteger('parent_id')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('parent_id')->references('id')->on('categories')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
Model: Category.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Category Model
*
* Represents a category in the system.
*
* @package App\Models
*
* @property bigint $id
* @property string $name
* @property string $slug
* @property string|null $description
* @property bigint|null $parent_id
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class Category extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
'description',
'parent_id',
];
/**
* Get the parent category.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id');
}
/**
* Get the subcategories for the category.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subcategories()
{
return $this->hasMany(Category::class, 'parent_id');
}
/**
* The posts that belong to the category.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function posts()
{
return $this->belongsToMany(Post::class, 'post_category');
}
}
Factory: CategoryFactory.php
php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* Category Factory
*
* @package Database\Factories
*/
class CategoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Category::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
$name = $this->faker->unique()->word();
return [
'name' => ucfirst($name),
'slug' => Str::slug($name),
'description' => $this->faker->optional()->sentence(),
'parent_id' => null, // Could set up a hierarchy.
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Module: Tags
Migration: create_tags_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateTagsTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
Model: Tag.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Tag Model
*
* Represents a tag in the system.
*
* @package App\Models
*
* @property bigint $id
* @property string $name
* @property string $slug
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class Tag extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
];
/**
* The posts that belong to the tag.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function posts()
{
return $this->belongsToMany(Post::class, 'post_tag');
}
}
Factory: TagFactory.php
php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* Tag Factory
*
* @package Database\Factories
*/
class TagFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Tag::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
$name = $this->faker->unique()->word();
return [
'name' => ucfirst($name),
'slug' => Str::slug($name),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Module: Blog Posts
Migration: create_posts_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreatePostsTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->string('slug')->unique();
$table->text('content');
$table->text('excerpt')->nullable();
$table->enum('status', ['draft', 'published', 'archived'])->default('draft');
$table->timestamp('published_at')->nullable();
$table->string('seo_title')->nullable();
$table->text('seo_description')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Model: Post.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Post Model
*
* Represents a blog post in the system.
*
* @package App\Models
*
* @property bigint $id
* @property bigint $user_id
* @property string $title
* @property string $slug
* @property string $content
* @property string|null $excerpt
* @property string $status
* @property timestamp|null $published_at
* @property string|null $seo_title
* @property string|null $seo_description
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class Post extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'title',
'slug',
'content',
'excerpt',
'status',
'published_at',
'seo_title',
'seo_description',
];
/**
* The user that owns the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* The categories that belong to the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categories()
{
return $this->belongsToMany(Category::class, 'post_category');
}
/**
* The tags that belong to the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany(Tag::class, 'post_tag');
}
/**
* Get the likes for the post.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function likes()
{
return $this->hasMany(Like::class);
}
/**
* Get the comments for the post.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
/**
* Get the media for the post.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function media()
{
return $this->hasMany(Media::class);
}
}
Factory: PostFactory.php
php
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* Post Factory
*
* @package Database\Factories
*/
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Post::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
$title = $this->faker->sentence();
return [
'user_id' => User::factory(),
'title' => $title,
'slug' => Str::slug($title) . '-' . $this->faker->unique()->numberBetween(1000, 9999),
'content' => $this->faker->paragraphs(3, true),
'excerpt' => $this->faker->optional()->paragraph(),
'status' => $this->faker->randomElement(['draft', 'published', 'archived']),
'published_at' => $this->faker->optional()->dateTime(),
'seo_title' => $this->faker->optional()->sentence(),
'seo_description' => $this->faker->optional()->paragraph(),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Module: Post_Category (Pivot Table)
Migration: create_post_category_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreatePostCategoryTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreatePostCategoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_category', function (Blueprint $table) {
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('category_id');
$table->timestamps();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->primary(['post_id', 'category_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('post_category');
}
}
Note: Pivot tables typically do not require Eloquent models unless additional functionality is needed.
Module: Post_Tag (Pivot Table)
Migration: create_post_tag_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreatePostTagTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreatePostTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('tag_id');
$table->timestamps();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
$table->primary(['post_id', 'tag_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('post_tag');
}
}
Note: Pivot tables typically do not require Eloquent models unless additional functionality is needed.
Module: Comments
Migration: create_comments_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateCommentsTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('user_id')->nullable();
$table->unsignedBigInteger('parent_id')->nullable();
$table->text('content');
$table->enum('status', ['approved', 'pending', 'spam'])->default('pending');
$table->timestamps();
$table->softDeletes();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('parent_id')->references('id')->on('comments')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Model: Comment.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Comment Model
*
* Represents a comment on a post.
*
* @package App\Models
*
* @property bigint $id
* @property bigint $post_id
* @property bigint|null $user_id
* @property bigint|null $parent_id
* @property string $content
* @property string $status
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class Comment extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'post_id',
'user_id',
'parent_id',
'content',
'status',
];
/**
* The post that the comment belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function post()
{
return $this->belongsTo(Post::class);
}
/**
* The user that made the comment.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* The parent comment if any.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
{
return $this->belongsTo(Comment::class, 'parent_id');
}
/**
* The replies to the comment.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
}
Factory: CommentFactory.php
php
<?php
namespace Database\Factories;
use App\Models\Post;
use App\Models\User;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* Comment Factory
*
* @package Database\Factories
*/
class CommentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Comment::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'post_id' => Post::factory(),
'user_id' => $this->faker->optional()->boolean(80) ? User::factory()->create()->id : null,
'parent_id' => null, // Can be set to create nested comments
'content' => $this->faker->sentence(),
'status' => $this->faker->randomElement(['approved', 'pending', 'spam']),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Module: Like
Migration: create_likes_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateLikesTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateLikesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('post_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->unique(['user_id', 'post_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('likes');
}
}
Model: Like.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* Like Model
*
* Represents a like on a post.
*
* @package App\Models
*
* @property bigint $id
* @property bigint $user_id
* @property bigint $post_id
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
*/
class Like extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'post_id',
];
/**
* The user that made the like.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* The post that was liked.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function post()
{
return $this->belongsTo(Post::class);
}
}
Factory: LikeFactory.php
php
<?php
namespace Database\Factories;
use App\Models\Like;
use App\Models\User;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* Like Factory
*
* @package Database\Factories
*/
class LikeFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Like::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'user_id' => User::factory(),
'post_id' => Post::factory(),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
];
}
}
Module: Media
Migration: create_media_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateMediaTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateMediaTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('media', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('post_id')->nullable();
$table->unsignedBigInteger('user_id');
$table->string('file_path');
$table->string('file_type');
$table->unsignedBigInteger('size');
$table->string('title')->nullable();
$table->string('alt_text')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('set null');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('media');
}
}
Model: Media.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Media Model
*
* Represents media files associated with posts or users.
*
* @package App\Models
*
* @property bigint $id
* @property bigint|null $post_id
* @property bigint $user_id
* @property string $file_path
* @property string $file_type
* @property bigint $size
* @property string|null $title
* @property string|null $alt_text
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class Media extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'post_id',
'user_id',
'file_path',
'file_type',
'size',
'title',
'alt_text',
];
/**
* The post that the media belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function post()
{
return $this->belongsTo(Post::class);
}
/**
* The user that uploaded the media.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Factory: MediaFactory.php
php
<?php
namespace Database\Factories;
use App\Models\Media;
use App\Models\Post;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* Media Factory
*
* @package Database\Factories
*/
class MediaFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Media::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'post_id' => $this->faker->optional()->boolean(70) ? Post::factory()->create()->id : null,
'user_id' => User::factory(),
'file_path' => $this->faker->filePath(),
'file_type' => $this->faker->fileExtension(),
'size' => $this->faker->numberBetween(1000, 1000000),
'title' => $this->faker->optional()->sentence(),
'alt_text' => $this->faker->optional()->sentence(),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Module: Reports
Migration: create_reports_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateReportsTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateReportsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('reports', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('description')->nullable();
$table->string('report_type');
$table->json('parameters')->nullable();
$table->timestamp('generated_at')->nullable();
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('reports');
}
}
Model: Report.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Report Model
*
* Represents a generated report in the system.
*
* @package App\Models
*
* @property bigint $id
* @property string $title
* @property string|null $description
* @property string $report_type
* @property array|null $parameters
* @property timestamp|null $generated_at
* @property bigint $user_id
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class Report extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'description',
'report_type',
'parameters',
'generated_at',
'user_id',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'parameters' => 'array',
'generated_at' => 'datetime',
];
/**
* The user that generated the report.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Factory: ReportFactory.php
php
<?php
namespace Database\Factories;
use App\Models\Report;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* Report Factory
*
* @package Database\Factories
*/
class ReportFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Report::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => $this->faker->sentence(),
'description' => $this->faker->optional()->paragraph(),
'report_type' => $this->faker->word(),
'parameters' => $this->faker->optional()->json(),
'generated_at' => $this->faker->optional()->dateTime(),
'user_id' => User::factory(),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Module: Settings
Migration: create_settings_table.php
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* CreateSettingsTable Migration
*
* @category Migration
* @package Database\Migrations
*/
class CreateSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('key')->unique();
$table->text('value')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}
Model: Setting.php
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Setting Model
*
* Represents a key-value setting in the system.
*
* @package App\Models
*
* @property bigint $id
* @property string $key
* @property string|null $value
* @property timestamp|null $created_at
* @property timestamp|null $updated_at
* @property timestamp|null $deleted_at
*/
class Setting extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'key',
'value',
];
}
Factory: SettingFactory.php
php
<?php
namespace Database\Factories;
use App\Models\Setting;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* Setting Factory
*
* @package Database\Factories
*/
class SettingFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Setting::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'key' => $this->faker->unique()->word(),
'value' => $this->faker->optional()->sentence(),
'created_at' => $this->faker->optional()->dateTime(),
'updated_at' => $this->faker->optional()->dateTime(),
'deleted_at' => null,
];
}
}
Summary of Relationships and Best Practices
Migrations: Each migration is responsible for creating its respective table with all necessary fields, constraints, and indexes. Foreign keys are properly defined to maintain referential integrity.
Models: Each Eloquent model uses the HasFactory and SoftDeletes traits where applicable. Relationships are clearly defined using Laravel's relationship methods (belongsTo, hasMany, belongsToMany, etc.). Mass assignable attributes are specified using the $fillable property to protect against mass assignment vulnerabilities.
Factories: Factories leverage Laravel's Faker library to generate realistic dummy data for seeding databases during testing or development. Optional fields are handled using Faker's optional() method.
PHPDoc Comments: Comprehensive PHPDoc comments are included for each class and method, detailing properties, return types, and purposes. This enhances code readability and maintainability.
Naming Conventions: Adheres to Laravel's naming conventions, using snake_case for table names and PascalCase for model names. Pivot tables are named alphabetically (e.g., post_category, post_tag) to avoid confusion.
Code Organization: Models are placed within the App\Models namespace, migrations within the database\migrations directory, and factories within the database\factories namespace. This organization facilitates easy navigation and maintenance.
SOLID Principles: Each class has a single responsibility, making the codebase easy to extend and maintain. Reusable components are abstracted appropriately, adhering to the Open/Closed and Single Responsibility principles.
DRY Principle: Common functionalities and patterns are reused across different modules to avoid redundancy, ensuring the code remains concise and maintainable.
By following these structured approaches, the codebase remains robust, scalable, and aligned with Laravel's best practices, ensuring an exceptional development and user experience.