omarsow94
1 year ago
21
21
Laravel

Delete post safely

Posted 1 year ago by omarsow94

I want to delete post , when i delete post who have not comments , it's work. But when a try to delete post with comments i have this error message :

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`akhmedou`.`users_ip`, CONSTRAINT `users_ip_article_id_foreign` FOREIGN KEY (`article_id`) REFERENCES `articles` (`id`)) (SQL: delete from `articles` where `id` = 3) ◀"

my function :

public function destroy($id)
     {
          $article = Article::find($id);

          $article->comments()->delete();

          $article->delete();
}

Please sign in or create an account to participate in this conversation.