Search code examples
laravellaravel-8database-migrationsoft-delete

Laravel 8 : SoftDeletes not updated when deleting a user linked to a form


I have a problem on Laravel 8 when I delete a user I have my 'deleted_at' which is updated in my 'user' table but not the 'deleted_at' of my 'forms' table because I want to delete the forms linked to this user otherwise I have an error because I display his info when he does not exist anymore. How do I fix this problem please?

I have used the soft delete in the Models and the 'deleted_at' is updated when I delete the form.

users migration :

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('firstname');
            $table->string('lastname');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable()->onDelete('cascade');
            $table->string('profile_photo_path', 2048)->nullable();
            $table->timestamps();
            $table->foreignId('role_id')->references('id')->on('roles')->onDelete('cascade');
            $table->softDeletes();
        });
    }

public function down()
    {
        Schema::dropIfExists('users', function (Blueprint $table) {
            $table->dropColumn('deleted_at');
        });
    }

forms migration :

public function up()
    {
        Schema::create('forms', function (Blueprint $table) {
            $table->id();
            $table->string('title', 100);
            $table->text('message');
            $table->datetime('date');
            $table->string('color');
            $table->softDeletes();
            $table->timestamps();
        });
    }


    public function down()
    {
        // Schema::dropIfExists('forms');
        Schema::table('forms', function (Blueprint $table) {
            $table->dropColumn('deleted_at');
        });
    }

add user to forms migration :

public function up()
    {
        Schema::table('forms', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }


    public function down()
    {
        Schema::table('forms', function (Blueprint $table) {
            $table->dropForeign('user_id')->onDelete('cascade');
        });
    }

Solution

  • Soft delete won't work on cascading foreign keys. Cascading foreign keys is a feature of sql servers and soft delete is a feature of laravel and needs the laravel context to work.

    When you delete it, for the soft deletes to work, you need to call delete on each model of the relation. This is quite easy with collections and higher order functions.

    {
        $user = User::firstOrFail($id);
    
        $user->forms->each->delete();
        $user->delete();
    
        ...
    }