My comment system is working well with polymorphic relation. But i need to explain something clearly to help me to solve this problem first.
There is a brand page and that brand page have comments, also brand has customer support and comments for it. I couldn't mind how to make this relationship with eloquent. Some brands don't have customer support so i need to return boolean to confirm this but i couldn't even prepare database for this relation.
Brand -> customerSupport -> Comments ( How to make this relationship and the database structure )
Do i need to add some extra columns to comments table to do that properly ? Or just need to make new modal as 'CustomerSupport' and import this to commentable_type while adding or listing comments ?
Table Structures
//Brands Table
id - name - slug - img - timestamps
//Comments Table
id - parent_id - user_id(fk) - commentable_id - commentable_type - timestamps
Brand Modal
class Brand extends Model
{
// Table Name
protected $table = 'brands';
// Primary Key
public $primaryKey = 'id';
public function comments()
{
return $this->morphMany('App\Comment', 'commentable')->whereNull('parent_id');
}
}
Comment Modal
class Comment extends Model
{
//Table Name
protected $table = 'comments';
// Primary Key
public $primaryKey = 'id';
//Fillables ...
public function commentable()
{
return $this->morphTo();
}
//Comment belongs to user
public function user()
{
return $this->belongsTo('App\User');
}
//Comment belongs to brand
public function brands()
{
return $this->belongsTo('App\Brand');
}
//Comment have many replies
public function replies()
{
return $this->hasMany('App\Comment', 'parent_id');
}
}
I added a customer_support column to the brand and comments table. With this way, I'm checking if the brand has customer support or not and then get the comments for each brand's customer support. I'm not sure this is the best way to do that relation.
Brand Modal
public function customerSupport()
{
return $this->morphMany('App\Comment', 'commentable')->whereNull('parent_id')
->whereCustomerSupport(true);
}