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

andremac96's avatar

Delete all attributes in one record except name and username.

Hi

I have a user table and user can delete their account and there data deletes. It's a soft delete. What I want is to delete all attributes except the name.

Can I do something like

$user->delete()->except(['name'']) ?

The record must be soft deleted just with the data removed.

0 likes
3 replies
Tray2's avatar

Not really knowing what it is you are trying to do but...

What you are doing is

DELETE FROM users WHERE id = $userId;

What you need to to is set the other fields to null if they are nullable.

$row = Model::find($id);
$row->column1 = null;
$row->column2 = null;
$row->update();
1 like
pardeepkumar's avatar

I think you try like this

$user = Test::where('id', 14)->where('username', 'like', '%dd%')->first();
$user->update(['username' => null]);

RonB1985's avatar

What Tray2 said. When you are using a delete action, it just deletes the record, but you still need to manually set the fields to null. You can do it like this as well:

$row = Model::find($id);
$row->update(['column1' => null, 'column2' => null]);
$row->delete();

So first you update the columns and then you delete the record.

You could also set a custom delete method in your model, where you perform the above actions, so you don't have to put it in the controller for example. Or you could also use a model observer:

    public static function boot ()
    {
        parent::boot();

        self::deleting(function ($model) {

            $model->update(['column1' => null, 'column2' => null]);
            $model->delete();

        });
    }

Or perhaps even something like:

    public static function boot ()
    {
        parent::boot();

        self::deleted(function ($model) {

            $model->update(['column1' => null, 'column2' => null]);

        });
    }

Not tested, but just something you could try, whatever suits your needs.

Please or to participate in this conversation.