Laravel删除所有数据透视表值

时间:2018-03-21 18:53:30

标签: laravel many-to-many

我有两个模型[标签和主题]。

标签:

class Tag extends Model{
    protected $fillable = ['name'];
    public function topics(){
        return $this -> belongsToMany('\App\Topic','tag_topic')->withPivot('topic_id', 'category_id');
    }
}

TOPIC:

class Topic extends Model{
    protected $fillable = [ 'user_id', 'creator_id', 'name', 'category_id'];

    public function tags(){
        return $this -> belongsToMany('\App\Tag', 'tag_topic')->withPivot('topic_id', 'category_id');
    }
}

然后我有两个表和数据透视表的迁移。 [TAG]

Schema::create('tags', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id')->unsigned()->unique();
        $table->string('name') -> unique();
        $table->timestamps();
    });

[课题]

Schema::create('topics', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id')->unsigned()->unique();
        $table->integer('user_id') -> unsigned();
        $table->integer('creator_id') -> unsigned();
        $table->string('name');
        $table->enum('status', ['pending', 'accepted']);
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade')->onUpdate('cascade');
        $table->foreign('creator_id')->references('id')->on('creators')->onDelete('cascade')->onUpdate('cascade');
        $table->foreign('user_id')->references('user_id')->on('creators')->onDelete('cascade')->onUpdate('cascade');
        $table->timestamps();
    });

[PIVOT]

Schema::create('tag_topic', function (Blueprint $table) {
        $table->increments('id')->unsigned()->unique();
        $table->integer('tag_id') -> unsigned() -> nullable();
        $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade')->onUpdate('cascade');
        $table->integer('topic_id') -> unsigned() -> nullable();
        $table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade')->onUpdate('cascade');
        $table->integer('category_id') -> unsigned() -> nullable();
        $table->foreign('category_id')->references('category_id')->on('topics')->onDelete('cascade')->onUpdate('cascade');
        $table->timestamps();
    });

我的问题是,一旦我删除任何TOPIC,无论哪一个,它都会自动删除所有内容,就像数据透视表一样。

我正在做的是为了删除主题:

$topic = Topic::where('name', '=', $request -> topic_name) -> first();
$category = Category::where('id', '=', $topic->category_id) -> first();
$topic -> delete();

但它删除了所有内容,任何帮助,我错过了什么?

1 个答案:

答案 0 :(得分:0)

我认为您的问题出现在主题架构上:

$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('creator_id')->references('id')->on('creators')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('user_id')->on('creators')->onDelete('cascade')->onUpdate('cascade');

您将从一个已删除的主题级联到任何用户,创建者或类别,然后这些用户,创建者或类别将级联回到与所有已删除的用户,创建者和类别相关的主题表,然后这些用户,创建者和类别进入循环直到整个表被删除。我删除了一些级联效果。