ok
Aug 7, 2019
5
Level 18
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
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();
});
}
Please or to participate in this conversation.