Michał
Michał

Reputation: 908

Laravel 5.5 Custom Soft Deletes on Model

My posts are defined as soft deleted or not via the value in the status column, where: 0 = unavailable, 1 = available, and 77 = soft deleted.

Currently, I add a global scope in the model to make sure I don't return soft-deleted posts:

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('status', function (Builder $builder) {
        $builder->where('status', '!=', '77');
    });
}

How would I modify softDelete (laravels built-in functionality) of a model from its default of timestamps and deleted_at columns to use my number/status based system, to work even/especially when using methods such as ->delete(), ->withTrashed(), and ->restore()?

Upvotes: 3

Views: 6122

Answers (3)

Sher Singh
Sher Singh

Reputation: 287

You can define a method in your repository like the following:

class YourRepository extends BaseRepository {

    public function withTrashed() {
        $this->model = $this->model->withTrashed();
        return $this;
     }
}

After that you can chain the same in your controllers:

$records = $this->repository->withTrashed()->all();

Upvotes: 0

N Mahurin
N Mahurin

Reputation: 1446

You can check out the model events part of Laravel. In your models, extend a baseModel class that you make. In this baseModel you can add an event that is triggered on model delete. Like so:

protected static function boot(){
    static::deleting(function($thisModel){
        $thisModel->attributes['status'] = 77;
        $thisModel->save();
        return false;
    });
}

When you return false, you stop the default operation for deleting the model. So this will instead set the status to 77 instead of removing it. Or you could just use that in any model you want to use this sort of deleting, instead of using a base model. I find the base model to be easier for large projects that have a few things that would implement status based soft deletes.

To expand on adding other soft-delete like functions to this model consider using local scopes instead of the listed global one. For example:

public function scopeOnlyTrashed(Builder $query){
    return $query->where('status', 77);
}

Now when you do the database call

Posts::onlyTrashed()->get();

you will get the same functionality as the laravel's onlyTrashed() method.

Upvotes: 3

Vladimir
Vladimir

Reputation: 1391

You need to insert into model usage class:

use Illuminate\Database\Eloquent\SoftDeletes;

Also in class declaration you need insert:

use SoftDeletes;

https://laravel.com/docs/5.5/eloquent#soft-deleting

Upvotes: 1

Related Questions