This tutorial is part of this mini project on topic oriented user community. To see all the sections of this project Click Here
This project will include the following tables.
1. users
2. communities
3. topics
4. community_topic
5. posts
6. comments
then the following snippets would need to be applied if we need to create migration schema of the above mentioned tables.
1. users
public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('is_admin')->default(0); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
2. communities
public function up() { Schema::create('communities', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained(); $table->string('name'); $table->string('slug'); $table->text('description'); $table->timestamps(); $table->softDeletes(); }); }
3. topics
public function up() { Schema::create('topics', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('slug'); $table->text('description'); $table->timestamps(); }); }
4. posts
public function up() { Schema::create('posts', function (Blueprint $table) { $table->id();
$table->foreignId('community_id')->constrained(); $table->foreignId('topic_id')->constrained(); $table->foreignId('user_id')->constrained(); $table->string('title'); $table->text('post_text')->nullable(); $table->timestamps(); $table->softDeletes(); }); }
5. community_topic ( pivot table )
public function up() { Schema::create('community_topic', function (Blueprint $table) { $table->foreignId('community_id')->constrained(); $table->foreignId('topic_id')->constrained(); }); }
6. comments
public function up() { Schema::create('comments', function (Blueprint $table) { $table->id(); $table->foreignId('post_id')->constrained(); $table->foreignId('user_id')->constrained(); $table->text('comment_text'); $table->timestamps(); }); }
Now that the migration has been set up, it's time to set up the packages.
1. Creation of Slug
composer require cviebrock/eloquent-sluggable
Let's move on to snippets for creating relationships between these tables in the appropriate model files after package setup.
1. Community.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Cviebrock\EloquentSluggable\Sluggable; class Community extends Model { use HasFactory, SoftDeletes, Sluggable; protected $fillable = ['user_id', 'name', 'description', 'slug']; public function topics() { return $this->belongsToMany(Topic::class); } /** * Return the sluggable configuration array for this model. * * @return array */ public function sluggable(): array { return [ 'slug' => [ 'source' => 'name' ] ]; } public function getRouteKeyName() { return 'slug'; } public function user() { return $this->belongsTo(User::class); } public function posts() { return $this->hasMany(Post::class); } }
2. Topic.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Cviebrock\EloquentSluggable\Sluggable; class Topic extends Model { use HasFactory, Sluggable; protected $fillable = ['name', 'description']; /** * Return the sluggable configuration array for this model. * * @return array */ public function sluggable(): array { return [ 'slug' => [ 'source' => 'name' ] ]; } public function getRouteKeyName() { return 'slug'; } public function posts() { return $this->hasMany(Post::class); } public function community() { return $this->belongsToMany(Community::class); } }
3. Post.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use HasFactory, SoftDeletes; protected $fillable = [ 'community_id', 'topic_id', 'user_id', 'title', 'post_text', 'post_image', 'post_url'];
public function community() { return $this->belongsTo(Community::class); } public function topic() { return $this->belongsTo(Topic::class); } public function user() { return $this->belongsTo(User::class); } public function comments() { return $this->hasMany(Comment::class)->latest(); } }
4. Comment.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use HasFactory; protected $fillable = ['post_id', 'user_id', 'comment_text']; public function user() { return $this->belongsTo(User::class); } }
Now, for the next post Click Here