1. Go to this page and download the library: Download notch/deleted-models library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
notch / deleted-models example snippets
$blogPost = BlogPost::find(5); // an Eloquent model
$blogPost->delete(); // values will be copied to the `deleted_models` table.
$blogPost = BlogPost::restore(5); // $blogPost will be restored and returned
return [
/*
* The model uses to store deleted models.
*/
'model' => Spatie\DeletedModels\Models\DeletedModel::class,
/*
* After this amount of days, the records in `deleted_models` will be deleted
*
* This functionality uses Laravel's native pruning feature.
*/
'prune_after_days' => 365,
];
use Illuminate\Database\Eloquent\Model;
use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels;
class BlogPost extends Model
{
use KeepsDeletedModels;
}
$blogPost = BlogPost::find(5);
$blogPost->delete(); // values will be copied to the `deleted_models` table.
$blogPost = BlogPost::restore(5); // $blogPost will be restored and returned
// $blogPost will be return, but it is not saved in the db yet
$blogPost = Blogpost::makeRestored($id);
$blogPost->save();
BlogPost::restoreQuietly(); // no events will be dispatched
use Illuminate\Database\Eloquent\Model;
use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels;
class BlogPost extends Model
{
use KeepsDeletedModels;
public static function beforeRestoringModel(DeletedModel $deletedModel): void
{
// this will be executed right before restoring a model
}
public static function afterRestoringModel(
Model $restoredMode,
DeletedModel $deletedModel
): void
{
// this will be executed right after restoring a model
}
}
use Illuminate\Database\Eloquent\Model;
use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels;
class BlogPost extends Model
{
use KeepsDeletedModels;
public function attributesToKeep(): array
{
// here you can customize which values should be kept. This is
// the default implementation.
return $this->toArray();
}
}
use Spatie\DeletedModels\Models\DeletedModel;
class CustomDeletedModel extends DeletedModel
{
protected function makeRestoredModel(string $modelClass): mixed
{
// add custom logic
return parent::makeRestoredModel($modelClass)
}
}