Hamid
Hamid

Reputation: 727

delete foreign key laravel 5.5

I have these tables : posts, users, tags, categories I know that when we delete a user, then we should delete posts that belong to that special user. I do it within my migration:

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

so I don't know if I remove a post, should I remove tags that belong to that post or same story for categories.... If I remove a post, what about post_id field in tags table? or categories??

Upvotes: 2

Views: 777

Answers (1)

Hanlin Wang
Hanlin Wang

Reputation: 779

yes, assuming post tag is many to many, and post category is many to many too, you should delete post_tag relationship and category_post relationship, but it's not exactly delete a tag or delete a category, it's just delete the record of relationship.

so you can add ->onDelete('cascade') on post_tag table and category_post tab if you are using default name convention.

Upvotes: 1

Related Questions