SoftDelete
trait SoftDelete (View source)
Soft delete support for Barry models.
Using this trait:
$model->delete()writes the current timestamp into thedeleted_atcolumn instead of physically removing the row.$model->restore()clearsdeleted_at.$model->forceDelete()performs a real DELETE.- Use the static query helpers
withTrashed(),withoutTrashed(), andonlyTrashed()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
Soft-delete this record by stamping the deleted_at column.
Restore a soft-deleted record by clearing its deleted_at column.
Force a physical DELETE that bypasses soft delete entirely.
Whether this instance has been soft-deleted.
Resolve the deleted_at column name, honouring an optional
protected string $deleted_at = '...'; override on the model.
Start a query that excludes soft-deleted rows.
Start a query that only returns soft-deleted rows.
Start a query that includes both active and soft-deleted rows.
Register a model.restoring listener.
Register a model.restored listener.
Register a model.forceDeleting listener.
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).
bool
restore()
Restore a soft-deleted record by clearing its deleted_at column.
Fires model.restoring / model.restored events. Returns true on
success.
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).
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.
User::withoutTrashed()->where('active', true)->get();
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.
This is equivalent to static::query() and is provided as a readable
intent marker.
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.