Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shadrix's avatar
Level 12

How to handle a dummy user to replace a user?

I'm not sure how to handle this the correct way.

Let us say we have messages table with dozens of messages from a user in a conversation.

Now, the user decides to leave the website and deletes his account. My "goal" is to completely delete the user from the user table (no soft-deleting) and update all relationships in all the tables I have. Especially, the messages table worries me, because it can be quite long.

How would you do this? Do you have one dummy user in the user table and just update all foreign keys to the dummy user?

Edit

Would this be enough? Where 1 would be a dummy user?

$table->foreign('user_id')->references('id')->on('users')->onDelete('set 1');
0 likes
4 replies
Snapey's avatar

I would leave the record alone, just overwrite the previous user's name with something like 'anon' and make up new values for the other fields.

I wouldn't go to the bother of reassigning all the conversations.

The conversations were private between the users? No need to redact one half of the conversation?

1 like
shadrix's avatar
Level 12

Good morning, thanks for your reply.

The conversations were private between the users? No need to redact one half of the conversation?

Yes, they are private. But since GDPR hit us I'm trying to delete everything that I don't need anymore.

I would leave the record alone, just overwrite the previous user's name with something like 'anon' and make up new values for the other fields.

I think I'm going to do it so. Thanks!

Edit

Another question that arises. A user has several tables like: user_privacies user_infosetc. Would you just go to every single table and set the attributes to null?

Currently, it would look like this:

'App\Events\DeleteUserWasCalled' => [
        'App\Listeners\User\DeleteAvatar',
        'App\Listeners\User\RemoveFromMailchimp',
        'App\Listeners\User\RemoveFromStripe',
        'App\Listeners\User\RemoveOffers',
        'App\Listeners\User\UpdateRelatedTablesToNull',
        'App\Listeners\User\RenameUser',
    ],

Edit 2

Another problem that arises is how the user table is built.

 $table->increments('id');
 $table->string('name')->unique();
 $table->string('email')->unique();
 $table->string('password');
 ...

The name must be unique, so "anon" cannot work out or should I use nullable and set the name to null?

martinbean's avatar

@shadrix You can update the user’s name to “Deleted User” or similar and then soft-delete the record.

To handle relationships in Eloquent models, you can define a “default” model:

public function user()
{
    return $this->belongsTo(User::class)
                ->withDefault(['name' => 'Deleted User']);
}
1 like

Please or to participate in this conversation.