trait SoftDelete (View source)

Soft delete support for Barry models.

Using this trait:

  • $model->delete() writes the current timestamp into the deleted_at column instead of physically removing the row.
  • $model->restore() clears deleted_at.
  • $model->forceDelete() performs a real DELETE.
  • Use the static query helpers withTrashed(), withoutTrashed(), and onlyTrashed() to scope your queries.

Schema requirement: the table must carry a nullable deleted_at TIMESTAMP column. Bow's migration helper $table->addSoftDelete() adds it.

The column name can be customised by declaring protected string $deleted_at = 'archived_on'; on the model.

Methods

int
delete()

Soft-delete this record by stamping the deleted_at column.

bool
restore()

Restore a soft-deleted record by clearing its deleted_at column.

int
forceDelete()

Force a physical DELETE that bypasses soft delete entirely.

bool
trashed()

Whether this instance has been soft-deleted.

string
getDeletedAtColumn()

Resolve the deleted_at column name, honouring an optional protected string $deleted_at = '...'; override on the model.

static Builder
withoutTrashed()

Start a query that excludes soft-deleted rows.

static Builder
onlyTrashed()

Start a query that only returns soft-deleted rows.

static Builder
withTrashed()

Start a query that includes both active and soft-deleted rows.

static void
restoring(callable $cb)

Register a model.restoring listener.

static void
restored(callable $cb)

Register a model.restored listener.

static void
forceDeleting(callable $cb)

Register a model.forceDeleting listener.

static void
forceDeleted(callable $cb)

Register a model.forceDeleted listener.

Details

int delete()

Soft-delete this record by stamping the deleted_at column.

Fires the standard model.deleting / model.deleted events so existing listeners keep working. Returns the number of affected rows (0 if the record had no primary-key value, was missing from the table, or is already trashed).

Return Value

int

bool restore()

Restore a soft-deleted record by clearing its deleted_at column.

Fires model.restoring / model.restored events. Returns true on success.

Return Value

bool

int forceDelete()

Force a physical DELETE that bypasses soft delete entirely.

Fires model.forceDeleting / model.forceDeleted (the standard model.deleting / model.deleted are NOT fired by this method — subscribe to the force-delete events when you need to react to it).

Return Value

int

bool trashed()

Whether this instance has been soft-deleted.

Return Value

bool

string getDeletedAtColumn()

Resolve the deleted_at column name, honouring an optional protected string $deleted_at = '...'; override on the model.

Return Value

string

static Builder withoutTrashed()

Start a query that excludes soft-deleted rows.

User::withoutTrashed()->where('active', true)->get();

Return Value

Builder

static Builder onlyTrashed()

Start a query that only returns soft-deleted rows.

Return Value

Builder

static Builder withTrashed()

Start a query that includes both active and soft-deleted rows.

This is equivalent to static::query() and is provided as a readable intent marker.

Return Value

Builder

static void restoring(callable $cb)

Register a model.restoring listener.

Parameters

callable $cb

Return Value

void

static void restored(callable $cb)

Register a model.restored listener.

Parameters

callable $cb

Return Value

void

static void forceDeleting(callable $cb)

Register a model.forceDeleting listener.

Parameters

callable $cb

Return Value

void

static void forceDeleted(callable $cb)

Register a model.forceDeleted listener.

Parameters

callable $cb

Return Value

void