пожалуйста, помогите мне... пожалуйста, помогите мне, когда я хочу опубликовать комментарий, отображается ошибка
SQLSTATE[HY000]: Общая ошибка: 1364 Поле 'parent_id' не имеет значения по умолчанию (SQL: вставить в comments
(comment
, user_id
, commentable_id
, commentable_type
, updated_at
, created_at
)
класс CommentController.php
public function store(Request $request)
{
$comment = new Comment;
$comment->comment = $request->comment;
$comment->user()->associate($request->user());
$post = Post::find($request->post_id);
$post->comments()->save($comment);
return back();
}
public function replyStore(Request $request)
{
$reply = new Comment();
$reply->comment = $request->get('comment');
$reply->user()->associate($request->user());
$reply->parent_id = $request->get('comment_id');
$post = Post::find($request->get('post_id'));
$post->comments()->save($reply);
return back();
}
модели Comment.php
use HasFactory;
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
модели Post.php
use HasFactory;
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable')->whereNull('parent_id');
}
2020_09_18_082706_create_comments_table.php
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('parent_id')->unsigned();
$table->text('comment');
$table->integer('commentable_id')->unsigned();
$table->string('commentable_type');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
[1]: https://i.stack.imgur.com/qObxM.png
parent_id
комментария, у которого нет родителя? также то, как это настроено, должно иметь значение - person lagbox   schedule 19.09.2020parent_id
или сделайте егоnullable()
в миграции. - person miken32   schedule 19.09.2020parent_id
, если у вас есть трансформируемое отношение к комментируемому объекту? - person miken32   schedule 19.09.2020