Laravel 5.5 Custom Soft Deletes on Model

筅森魡賤 提交于 2020-01-04 03:51:32

问题


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()?


回答1:


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.




回答2:


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




回答3:


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();


来源:https://stackoverflow.com/questions/48190933/laravel-5-5-custom-soft-deletes-on-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!