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

ajithlal's avatar

Update status field value while soft deleting a record

Hi,

I'm using softDelete to delete a record. I want to update my status field value before soft deleting the record. Here is my delete code.

public function destroy($id)
    {
        try {
            $booking = Booking::findOrFail($id);
            $booking->status = Booking::STATUS_DELETED;
            if ($booking->save()) {
                $booking->delete();
            }

            return redirect()->route('bookings.booking.index')
                ->with('success_message', trans('bookings.model_was_deleted'));
        } catch (Exception $exception) {
            return back()->withInput()
                ->withErrors(['unexpected_error' => trans('bookings.unexpected_error')]);
        }
    }

but the status is not changing

0 likes
5 replies
ajithlal's avatar
ajithlal
OP
Best Answer
Level 18

Thanks. That has been fixed by overriding the boot method like this.

public static function boot()
    {
        parent::boot();
        self::deleting(function ($model) {
            $model->status = static::STATUS_DELETED;
            $model->save();
        });
    }
click's avatar

@ajithlal your question was missing... Looking at your code I don't see a reason why it shouldn't work. Is your model marked as deleted? Is deleted_at populated after you run this code?

Besides that, I think there is a nicer way to resolve this by using the model event: deleting.

https://laravel.com/docs/5.8/eloquent#events

# in your model
public static function boot()
{
  parent::boot();

  static::deleting(function($model)) {
     /** @var static $model */
     $model->status = static::STATUS_DELETED; 
  }

}

And than you can only do: $booking->delete()

1 like
ajithlal's avatar

@click the deleted_at is populated after i run the code. but the status is not changing. So i do the status changing in deleting event.

I've solved it already.

thanks for your support :)

Please or to participate in this conversation.