Delete post safely

Published 2 weeks 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();
}
Snapey
Snapey
2 weeks ago (779,535 XP)

are you using softdeletes?

tykus
tykus
2 weeks ago (432,930 XP)

What happens as a result of this line:

$article->comments()->delete();
Snapey
Snapey
2 weeks ago (779,535 XP)

the error points to some connection to users_ip

what is this?

omarsow94

@Snapey i have "users_ip" table to insert all users IP adress who view article

omarsow94

@tykus

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

delete comments it's work , but after when a try to delete $article i have error

tykus
tykus
2 weeks ago (432,930 XP)

delete comments it's work

So, the comments records are removed from the table or soft deleted?

omarsow94

@tykus removed

tykus
tykus
2 weeks ago (432,930 XP)

What is the user_ip field?

omarsow94

I don't have a user_ip field in my articles migration

public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('body');
            $table->integer('visible');
            $table->integer('check')->default(0);
            $table->integer('view_counter')->default(0);
            $table->string('cover')->default(URL::to('/').'/uploads/covers/default.jpg');
            $table->timestamps();
            $table->foreign('user_id')->references('id')
               ->on('users')->onDelete('cascade');
        });
    }

@tykus

tykus
tykus
2 weeks ago (432,930 XP)

So what is the akhmedou table?

omarsow94

akhmedou is not a table , it's the database name . @tykus

tykus
tykus
2 weeks ago (432,930 XP)

Ok, what then is user_ip - the users_ip_article_id_foreign is preventing you from deleting the article?

omarsow94

exactly @tykus , but my function work ?

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

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

          $article->delete();
}
tykus
tykus
2 weeks ago (432,930 XP)

You have a user_ip table? With an article_id field? With a foreign key constraint referencing the id field on articles?

That's your problem. Delete the associated user_ip record before attempting to delete the article.

omarsow94

Oh yeah I had not thought about that, Thank you very much @tykus

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