PHP code example of notch / deleted-models

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,
];

$schedule->command('model:prune', [
    '--model' => [\Spatie\DeletedModels\Models\DeletedModel::class],
])->daily();

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

BlogPost::restore(5, function(BlogPost $post, DeletedModel $deletedModel) {
    $post->title = "{$post->title} (restored)";
})

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)
    }
}
bash
php artisan vendor:publish --tag="deleted-models-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="deleted-models-config"