Certainly! Below is a comprehensive implementation of the specified modules using Laravel's best practices. Each module includes the necessary migrations and Eloquent models with detailed PHPDoc comments. The code adheres to naming conventions, SOLID principles, and the DRY (Don't Repeat Yourself) principle to ensure maintainability and scalability.
Table of Contents
- Role Management Module
- User Management Module
- Category Management Module
- Tag Management Module
- Post Management Module
- Comment Management Module
- Analytics Management Module
Role Management Module
Roles Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug')->unique();
$table->timestamps(); // Includes 'created_at' and 'updated_at'
$table->softDeletes(); // Adds 'deleted_at' column
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
Role Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* Class Role
*
* @package App\Models
*
* @property int $id
* @property string $name
* @property string $slug
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
*
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users
*/
class Role extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
];
/**
* The users that belong to the role.
*
* @return BelongsToMany
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'role_user');
}
}
RoleUser Pivot Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRoleUserPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->primary(['role_id', 'user_id']);
// Foreign Key Constraints
$table->foreign('role_id')
->references('id')->on('roles')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
User Management Module
Users Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('avatar')->nullable();
$table->text('bio')->nullable();
// Note: 'role_id' is not needed since roles are managed via many-to-many
$table->timestamps(); // Includes 'created_at' and 'updated_at'
$table->softDeletes(); // Adds 'deleted_at' column
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('users');
}
}
User Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class User
*
* @package App\Models
*
* @property int $id
* @property string $name
* @property string $email
* @property string $password
* @property string|null $avatar
* @property string|null $bio
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
*
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Role[] $roles
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Post[] $posts
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
*/
class User extends Authenticatable
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'avatar',
'bio',
];
/**
* The roles that belong to the user.
*
* @return BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'role_user');
}
/**
* Get the posts for the user.
*
* @return HasMany
*/
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
/**
* Get the comments for the user.
*
* @return HasMany
*/
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
}
Category Management Module
Categories Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug')->unique();
$table->text('description')->nullable();
$table->timestamps(); // Includes 'created_at' and 'updated_at'
$table->softDeletes(); // Adds 'deleted_at' column
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
Category Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class Category
*
* @package App\Models
*
* @property int $id
* @property string $name
* @property string $slug
* @property string|null $description
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
*
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Post[] $posts
*/
class Category extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
'description',
];
/**
* Get the posts for the category.
*
* @return HasMany
*/
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
}
Tag Management Module
Tags Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug')->unique();
$table->timestamps(); // Includes 'created_at' and 'updated_at'
$table->softDeletes(); // Adds 'deleted_at' column
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
Tag Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* Class Tag
*
* @package App\Models
*
* @property int $id
* @property string $name
* @property string $slug
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
*
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Post[] $posts
*/
class Tag extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
];
/**
* The posts that belong to the tag.
*
* @return BelongsToMany
*/
public function posts(): BelongsToMany
{
return $this->belongsToMany(Post::class, 'post_tag');
}
}
Post Management Module
Posts Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->string('title');
$table->string('slug')->unique();
$table->text('content');
$table->text('excerpt')->nullable();
$table->string('featured_image')->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(); // Includes 'created_at' and 'updated_at'
$table->softDeletes(); // Adds 'deleted_at' column
// Foreign Key Constraints
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('category_id')
->references('id')->on('categories')
->onDelete('set null'); // Optional: Handle delete behavior as per requirements
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Post Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* Class Post
*
* @package App\Models
*
* @property int $id
* @property int $user_id
* @property int $category_id
* @property string $title
* @property string $slug
* @property string $content
* @property string|null $excerpt
* @property string|null $featured_image
* @property string $status
* @property \Illuminate\Support\Carbon|null $published_at
* @property string|null $seo_title
* @property string|null $seo_description
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
*
* @property-read \App\Models\User $user
* @property-read \App\Models\Category $category
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Like[] $likes
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Tag[] $tags
*/
class Post extends Model
{
use SoftDeletes;
/**
* The valid status options.
*/
public const STATUS_DRAFT = 'draft';
public const STATUS_PUBLISHED = 'published';
public const STATUS_ARCHIVED = 'archived';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'category_id',
'title',
'slug',
'content',
'excerpt',
'featured_image',
'status',
'published_at',
'seo_title',
'seo_description',
];
/**
* The user that owns the post.
*
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* The category that the post belongs to.
*
* @return BelongsTo
*/
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
/**
* Get the comments for the post.
*
* @return HasMany
*/
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
/**
* Get the likes for the post.
*
* @return HasMany
*/
public function likes(): HasMany
{
return $this->hasMany(Like::class);
}
/**
* The tags that belong to the post.
*
* @return BelongsToMany
*/
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class, 'post_tag');
}
}
PostTag Pivot Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostTagPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('tag_id');
$table->primary(['post_id', 'tag_id']);
// Foreign Key Constraints
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
$table->foreign('tag_id')
->references('id')->on('tags')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('post_tag');
}
}
Comment Management Module
Comments Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('parent_id')->nullable();
$table->text('content');
$table->enum('status', ['pending', 'approved', 'rejected'])->default('pending');
$table->timestamps(); // Includes 'created_at' and 'updated_at'
$table->softDeletes(); // Adds 'deleted_at' column
// Foreign Key Constraints
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
$table->foreign('parent_id')
->references('id')->on('comments')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Comment Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class Comment
*
* @package App\Models
*
* @property int $id
* @property int $user_id
* @property int $post_id
* @property int|null $parent_id
* @property string $content
* @property string $status
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
*
* @property-read \App\Models\User $user
* @property-read \App\Models\Post $post
* @property-read \App\Models\Comment|null $parent
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $children
*/
class Comment extends Model
{
use SoftDeletes;
/**
* The valid status options.
*/
public const STATUS_PENDING = 'pending';
public const STATUS_APPROVED = 'approved';
public const STATUS_REJECTED = 'rejected';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'post_id',
'parent_id',
'content',
'status',
];
/**
* The user that owns the comment.
*
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* The post that the comment belongs to.
*
* @return BelongsTo
*/
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
/**
* The parent comment if any.
*
* @return BelongsTo
*/
public function parent(): BelongsTo
{
return $this->belongsTo(Comment::class, 'parent_id');
}
/**
* The child comments of this comment.
*
* @return HasMany
*/
public function children(): HasMany
{
return $this->hasMany(Comment::class, 'parent_id');
}
}
Analytics Management Module
Analytics Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAnalyticsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('analytics', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('post_id');
$table->integer('views_count')->default(0);
$table->integer('likes_count')->default(0);
$table->integer('comments_count')->default(0);
$table->timestamps(); // Includes 'created_at' and 'updated_at'
// Foreign Key Constraints
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('analytics');
}
}
Analytics Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Class Analytics
*
* @package App\Models
*
* @property int $id
* @property int $post_id
* @property int $views_count
* @property int $likes_count
* @property int $comments_count
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*
* @property-read \App\Models\Post $post
*/
class Analytics extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'analytics';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'post_id',
'views_count',
'likes_count',
'comments_count',
];
/**
* The post that the analytics belong to.
*
* @return BelongsTo
*/
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
}
Additional Considerations
Like Management Module
The original schema mentions that a post has many likes. Depending on the application's requirements, you might need to implement a likes table and corresponding Like model. Below is an example implementation.
Likes Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLikesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('post_id');
$table->timestamps();
// Foreign Key Constraints
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
// Ensure a user can like a post only once
$table->unique(['user_id', 'post_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
* @throws \Illuminate\Database\Schema\Blueprint|\Illuminate\Support\Facades\Schema
*/
public function down()
{
Schema::dropIfExists('likes');
}
}
Like Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Class Like
*
* @package App\Models
*
* @property int $id
* @property int $user_id
* @property int $post_id
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*
* @property-read \App\Models\User $user
* @property-read \App\Models\Post $post
*/
class Like extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'post_id',
];
/**
* The user that made the like.
*
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* The post that was liked.
*
* @return BelongsTo
*/
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
}
Final Notes
- Soft Deletes: Implemented in models where records can be soft deleted to maintain data integrity.
- Mass Assignment Protection: Carefully specified
$fillable properties in models to prevent mass assignment vulnerabilities.
- Laravel Naming Conventions: Followed Laravel’s naming conventions for tables (snake_case, plural) and models (PascalCase, singular).
- Relationships: Defined using Eloquent's relationship methods (
belongsTo, hasMany, belongsToMany), facilitating intuitive data access and manipulation.
- Pivot Tables: Created for many-to-many relationships (
role_user, post_tag) without separate models unless additional functionality is required.
- Enums for Status Fields: Utilized PHP constants in models to represent enum values, enhancing code readability and maintainability.
- Foreign Key Constraints: Ensured referential integrity through foreign key constraints with appropriate
onDelete behaviors.
This implementation provides a robust foundation for the application's backend, ensuring scalability and ease of maintenance. Further enhancements can include adding scopes, accessors/mutators, and other Eloquent features as required by the business logic.