PHP code example of hith / laravel-eraser

1. Go to this page and download the library: Download hith/laravel-eraser 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/ */

    

hith / laravel-eraser example snippets


use Eraser\Traits\HasManualEraser;

class Post extends Model
{
    use HasManualEraser;

    public array $erasable = ['comments','tags'];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(Comment::class);
    }
}


$post->comments()->delete();
$post->tags()->detach();
$post->delete();

$post->delete();

$post->clean();

use Eraser\Traits\HasAutoEraser;

class Post extends Model
{
    use HasAutoEraser;

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(Comment::class);
    }
}


$post->clean();
// or
$post->delete();

use Eraser\Eraser;

$eraser = new Eraser();    // Manual is the default
$eraser->clean($model);    // cleans relations only
$eraser->delete($model);   // or cleans relations and deletes model


use Eraser\Eraser;

$eraser = new Eraser('auto');    // set eraser to auto
$eraser->clean($model);
$eraser->delete($model);


use Eraser\Facades\Erase;

Erase::clean($model);   // Manual is the default
Erase::delete($model);  // or cleans relations and deletes model

// Erase Facade default mode is manual
// use `type()` method to set type
// types: [manual,auto]
Erase::type('auto')->clean($model);


/**
* Determines whether the "eraser" should automatically start deleting
* related models when the parent model is being deleted.
* Default: true
* */
public bool $eraser_onDeleteStart = true;

/**
 * Controls whether relations to be deleted should be automatically
 * discovered when using the Auto Eraser functionality.
 * Default: true
 * */
public bool $eraser_autoDiscover = true;

/**
 * Enables or disables logging for eraser operations.
 * Default: true
 * */
public bool $eraser_logging = false;

/**
 * Set logger for model logs
 * */
public callable|LoggerInterface $logger;


public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}

public function posts(): HasMany
{
    return $this->hasMany(Post::class);
}

public function tags(): BelongsToMany
{
    return $this->belongsToMany(Tag::class);
}
bash
php artisan vendor:publish --tag=eraser-config