0

请帮助我...当我想发表评论时请帮助我显示错误

SQLSTATE [HY000]:一般错误:1364 字段“parent_id ”没有默认值(SQL :插入comments(、、、、、、commentuser_idcommentable_idcommentable_typeupdated_atcreated_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
4

0 回答 0