Я создаю свой собственный форум в качестве практики, я настраиваю Topic
и Comment
модели с полиморфными отношениями, comment
может принадлежать topic
или другому comment
, это потому, что я хочуиметь вложенные комментарии. Другая проблема заключается в том, что при ответе на тему я получаю следующую ошибку:
SQLSTATE [HY000]: общая ошибка: 1364 Поле 'parent_id' не имеет значения по умолчанию (SQL: вставить в comments
(user_id
, body
, commentable_id
, commentable_type
, updated_at
, created_at
) значений (1 <\ p> ljlnlnlnlnlnllnlnlnlnlnlnl
, 1, App \ Models \ Topic, 2019-11-08 20:41:43, 2019-11-08 20:41:43))
Мои модели и миграции.
Модель темы:
class Topic extends Model
{
protected $table = 'topics';
public function author()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable')->whereNull('parent_id');
}
}
public function up()
{
Schema::create('topics', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users');
$table->string('title');
$table->text('body');
$table->string('url')->unique();
$table->string('slug')->unique();
$table->boolean('isVisible')->default(false);
$table->timestamps();
});
}
Комментарий модели:
class Comment extends Model
{
protected $table = 'comments';
public function author()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
public function replies()
{
return $this->hasMany('App\Models\Comment', 'parent_id');
}
}
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->integer('parent_id')->unsigned();
$table->integer('commentable_id')->unsigned();
$table->string('commentable_type');
$table->text('body');
$table->timestamps();
});
}