Home > Software design >  Laravel 8 : SoftDeletes not updated when deleting a user linked to a form
Laravel 8 : SoftDeletes not updated when deleting a user linked to a form

Time:11-28

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');
        });
    }

CodePudding user response:

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();

    ...
}

CodePudding user response:

Thank you very much mrhn, Thanks to you I have learned a lot!

For those which want the function in the controller which thus makes it possible to remove the user to which belongs thus the id and one thus removes also what is related to him in my case a forms (function in the Models for the cardinalities between entity) :

public function destroy($id)
    {
        // $users = User::findOrFail($id);
        // $users->delete();

        $user = User::findOrFail($id);
        $user->forms->each->delete();
        $user->delete();

        return redirect()->route('admin');
    }
  • findOrFail and not firstOrFail ^^'
  • Related