/** * Class SoftDeletingScope * @package App */ classSoftDeletingScopeextendsSystemSoftDeletingScope { /** * Default value when the resource is not soft deleted * * @var int */ const NOT_DELETE_DEFAULT_VALUE = 0;
/** * Apply the scope to a given Eloquent query builder. * Filter soft deleted data by default * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * * @return void */ publicfunctionapply(Builder $builder, Model $model) { $builder->where($model->getQualifiedDeletedAtColumn(), self::NOT_DELETE_DEFAULT_VALUE); }
/** * Add the restore extension to the builder. * Add restore extensions to restore soft deleted data back to normal data * * @param \Illuminate\Database\Eloquent\Builder $builder * * @return void */ protectedfunctionaddRestore(Builder $builder) { $builder->macro('restore', function (Builder $builder) { $builder->withTrashed();
/** * Add the without-trashed extension to the builder. * Filtering data that has been soft deleted * * @param \Illuminate\Database\Eloquent\Builder $builder * * @return void */ protectedfunctionaddWithoutTrashed(Builder $builder) { $builder->macro('withoutTrashed', function (Builder $builder) { $model = $builder->getModel();
/** * Add the only-trashed extension to the builder. * Add an extension to retrieve only data that has been soft deleted * * @param \Illuminate\Database\Eloquent\Builder $builder * * @return void */ protectedfunctionaddOnlyTrashed(Builder $builder) { $builder->macro('onlyTrashed', function (Builder $builder) { $model = $builder->getModel();
/** * Class Test * @package App */ classTestextendsModel { /** Introduced framework native soft deletes */ useSoftDeletes;
/** * Modify the framework native soft delete lock dependent * global scope to be custom scope */ publicstaticfunctionbootSoftDeletes() { static::addGlobalScope(new SoftDeletingScope); }
/** * The table associated with the model. * * @var string */ protected$table = 'test'; /** * The storage format of the model's date columns. * * @var string */ protected$dateFormat = 'U'; }