What does onDelete('cascade') mean?

后端 未结 4 1363
说谎
说谎 2021-02-02 10:00
Schema::table(\'posts\', function (Blueprint $table) {
    $table->integer(\'user_id\')->unsigned();
    $table->foreign(\'user_id\')->references(\'id\')->         


        
4条回答
  •  没有蜡笔的小新
    2021-02-02 10:49

    Short answer is: in your case, if you deleted a user, all posts related to him will be deleted too.

    onDelete('cascade'); simply adds ON DELETE CASCADE rule to your database which specifies that the child data gets deleted when the parent data is deleted.

    Note: take care of the typo (double semicolon)

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

提交回复
热议问题