I have a Table Named Comments with following structure
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->morphs('commentable');
$table->morphs('creatable');
$table->text('comment');
$table->timestamps();
});
Eloquent File
class Comment extends Model
{
public $fillable = ['comment'];
public function commentable()
{
return $this->morphTo();
}
public function creatable()
{
return $this->morphTo();
}
}
where I have two polymorphic relationships
commentable for any Article/Post or a Video
creatable for the comment Creator of the comment User/Admin
How to add comment against a Post created by a user?
I tried creating using following code
public function addComment($creatable, $comment)
{
$this->comments()->create(['comment' => $comment, 'creatable' => $creatable]);
}
It did work, I received following Error
Illuminate/Database/QueryException with message 'SQLSTATE[HY000]: General error: 1364 Field 'creatable_type' doesn't have a default value (SQL: insert into `post_comments` (`comment`, `commentable_id`, `commentable_type`, `updated_at`, `created_at`) values (Test Comment, 1, App/Post, 2018-08-31 10:29:14, 2018-08-31 10:29:14))'
Thanks in advance!!!