Tom Headifen
Tom Headifen

Reputation: 1996

Overwriting soft delete table column for a model

I'm using laravel 5.4.

I have a model that points to a database table that has the following attributes.

id       integer
name     varchar
location varchar
deleted  boolean

This is an old table and due to it being entwined in a bunch of code we can't change this table.

What I want to do is take advantage of laravels soft delete functionality within a model. I know that laravel expects the column name to be deleted_at and the data type to be date on the database table.

How do I override this so this so that Laravel will look at the column deleted and check if it's a boolean?

I've tried looking at the classes that my model inherits from but haven't been able to find where the constant and function that handles this is defined. I suspect it might be on the collection but have had no luck.

Ideally I would like to simply redefine the functions in my model.

Thanks.

Upvotes: 4

Views: 3131

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Look into the Illuminate\Database\Eloquent\SoftDeletes trait and override runSoftDelete() and getDeletedAtColumn methods in the model.

In runSoftDelete() use boolean instead of timestamp and in getDeletedAtColumn() use DELETED instead of DELETED_AT

Another solution is adding global scopes or local scopes to work with soft deleted data.

Upvotes: 6

Related Questions