Download the PHP package spatie/laravel-deleted-models without Composer

On this page you can find all versions of the php package spatie/laravel-deleted-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-deleted-models

Automatically copy deleted records to a separate table

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

When deleting an Eloquent model, this package will copy the attributes of that model to a new table called deleted_models. You can view this as a sort of "Recycle bin for models".

To restore a previous model you can call restore and pass the id.

This way of preserving information when deleting can be seen as an alternative to soft deletes. You can read more on the trade-offs in this blog post.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

To create the deleted_models table, you can publish and run the migrations with:

Optionally, you can publish the config file with:

This is the contents of the published config file:

The pruning of the deleted_models table depends on Laravel's native pruning feature. Don't forget to schedule the model:prune as instructed in Laravel's docs.

Usage

You should add the KeepsDeletedModels trait to all models whose attributes should be written to deleted_models whenever the model is deleted.

With this in place, the attributes will be written to deleted_models when the model is deleted.

To restore a previous model you can call restore and pass the id.

If the model to be restored can't be found in the deleted_models table, a Spatie\DeletedModels\Exceptions\NoModelFoundToRestore exception will be thrown.

Restoring without saving

To restore in memory, without actually saving it, you can call makeRestored. Keep in mind that calling this method will also remove the record in the deleted_models_table.

If the model to be restored can't be found in the deleted_models table, null will be returned by makeRestored.

Restoring without emitting events

By default, the Spatie\DeletedModels\Events\RestoringDeletedModelEvent and Spatie\DeletedModels\Events\DeletedModelEvent will be dispatched when calling restore on a model.

If you don't want these events to be dispatched, call restoreQuietly.

Customizing the restore process

Using closures passed to restore

The restore function accepts a callable as the second argument. The beforeSaving callable will be executed when the restored model was created in memory, but before saving it in the db.

Using methods on the model

If you need to run some logic to before and after restoring a model, you can implement beforeRestoringModel and afterRestoringModel on your model.

To determine which attributes and values should be kept in deleted_models, you can implement attributesToKeep

Pruning deleted models

After a while, the deleted_models table can become large. The DeletedModel implements Laravel's native MassPrunable trait.

You can configure the number of days records in the deleted_models will be pruned in the prune_after_days key of the deleted-models.php config file. By default, all deleted models will be kept for 365 days.

Don't forget to schedule the model:prune as instructed in Laravel's docs.

Low-level customization of the delete and restoration process

The DeletedModel model implements most logic to keep and restore deleted models. You can modify any of the behaviour by creating a class that extends Spatie\DeletedModels\Models\DeletedModel. You should put the class name of your extended class in the model key of the deleted-models.php config file.

With this in place you can override any of the methods in DeletedModel.

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

This package was inspired by these two blog posts:

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-deleted-models with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/contracts Version ^9.0|^10
spatie/laravel-package-tools Version ^1.13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/laravel-deleted-models contains the following files

Loading the files please wait ....