PHP 9 - Laravel Blog Website (CMS-Model)

By Sheldon L Published at 2020-09-10 Updated at 2020-09-10


Create Models and Migrations

php artisan make:model Category -m
php artisan make:model Author -m
php artisan make:model Post -m
php artisan make:model Photo -m
php artisan make:model Tag -m

php artisan make:model AuthorPost -m
php artisan make:model TagPost -m
php artisan make:model TagPhoto -m

Models and Migrations

Category

use HasTranslations;

protected $fillable = ['admin_id', 'thumbnail', 'name', 'slug', 'is_column', 'category_id', 'is_published'];

public $translatable = ['name'];

public function admin()
{
    return $this->belongsTo(Admin::class);
}

public function category()
{
    return $this->belongsTo(Category::class);
}

public function categories()
{
    return $this->hasMany(Category::class);
}

public function posts()
{
    return $this->hasMany(Post::class);
}
Schema::create('categories', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->unsignedBigInteger('admin_id');
  $table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');

  # self refrence
  $table->unsignedBigInteger('category_id')->nullable();
  $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

  $table->string('thumbnail')->nullable();
  $table->string('name')->unique();        # translatable
  $table->string('slug')->unique();

  $table->enum('is_column', ['0', '1']);   # column hasMany categories
  $table->enum('is_published', ['0', '1']);
});

Author

use HasTranslations;

protected $fillable = ['admin_id', 'thumbnail', 'name', 'slug', 'intro', 'is_published'];

public $translatable = ['name', 'intro'];

public function admin()
{
    return $this->belongsTo(Admin::class);
}

public function posts()
{
    return $this->belongsToMany(Post::class, 'author_posts');
}
Schema::create('authors', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->unsignedBigInteger('admin_id');
  $table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');

  $table->string('thumbnail')->nullable();
  $table->string('name')->unique();         # translatable
  $table->string('slug')->unique();
  $table->text('intro')->nullable();        # translatable

  $table->enum('is_published', ['0', '1']);

  # Many to Many: Post
});

Post

use HasTranslations;
protected $fillable = [
  'admin_id', 'thumbnail', 'title', 'slug', 'sub_title',
  'is_top', 'is_reproduced', 'source', 'source_url', 'editor',
  'intro', 'details', 'post_type', 'is_published',
];
public $translatable = ['title', 'sub_title', 'intro', 'details'];

public function admin()
{
  return $this->belongsTo(Admin::class);
}

public function category()
{
  return $this->belongsTo(Category::class);
}

public function authors()
{
  return $this->belongsToMany(Author::class, 'author_posts');
}

public function tags()
{
  return $this->belongsToMany(Tag::class, 'tag_posts');
}
Schema::create('posts', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->unsignedBigInteger('admin_id');
  $table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');

  # belongsTo: Category
  $table->unsignedBigInteger('category_id')->nullable();
  $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

  $table->string('thumbnail')->nullable();
  $table->string('title')->unique();        # translatable
  $table->string('slug')->unique();
  $table->string('sub_title')->nullable();  # translatable

  $table->enum('is_top', ['0', '1']);
  $table->enum('limit', ['0', '1']);

  $table->enum('is_reproduced', ['0', '1']);
  $table->string('source')->nullable();
  $table->text('source_url')->nullable();

  $table->string('editor')->nullable();
  $table->text('intro')->nullable();        # translatable
  $table->text('details');                  # translatable
  $table->string('post_type');

  $table->enum('is_published', ['0', '1']);

  # Many to Many: [Author, Tag]
});

Photo

use HasTranslations;

protected $fillable = ['admin_id', 'image_url', 'intro', 'is_published'];

public $translatable = ['intro'];

public function admin()
{
  return $this->belongsTo(Admin::class);
}

public function tags()
{
  return $this->belongsToMany(Tag::class, 'tag_photos');
}
Schema::create('photos', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->unsignedBigInteger('admin_id');
  $table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');

  $table->string('image_url')->unique();
  $table->text('intro')->nullable();    # translatable

  $table->enum('is_published', ['0', '1']);

  # Many to Many: Tag
});

Tag

use HasTranslations;

protected $fillable = ['admin_id', 'name', 'slug'];

public $translatable = ['name'];

public function admin()
{
    return $this->belongsTo(Admin::class);
}

public function posts()
{
    return $this->belongsToMany(Post::class, 'tag_posts');
}

public function photos()
{
    return $this->belongsToMany(Photo::class, 'tag_photos');
}
Schema::create('tags', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->unsignedBigInteger('admin_id');
  $table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');

  $table->string('name')->unique();    # translatable
  $table->string('slug')->unique();
});

AuthorPost

protected $fillable = ['author_id', 'post_id'];
Schema::create('author_posts', function (Blueprint $table) {
  $table->id();
  $table->timestamps();

  $table->unsignedBigInteger('author_id');
  $table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
  $table->unsignedBigInteger('post_id');
  $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});

TagPost

protected $fillable = ['tag_id', 'post_id'];
Schema::create('tag_posts', function (Blueprint $table) {
  $table->id();
  $table->timestamps();

  $table->unsignedBigInteger('tag_id');
  $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
  $table->unsignedBigInteger('post_id');
  $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});

TagPhoto

protected $fillable = ['tag_id', 'photo_id'];
Schema::create('tag_photos', function (Blueprint $table) {
  $table->id();
  $table->timestamps();

  $table->unsignedBigInteger('tag_id');
  $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
  $table->unsignedBigInteger('photo_id');
  $table->foreign('photo_id')->references('id')->on('photos')->onDelete('cascade');
});