PHP code example of ramiroestrella / laravel-database-anonymize

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

    

ramiroestrella / laravel-database-anonymize example snippets


return [
    'locale' => 'en_US',
    'chunk_size' => 1000,

    /*
    |--------------------------------------------------------------------------
    | Restricted Environments
    |--------------------------------------------------------------------------
    | Define environments (e.g., production, staging) that  Optionally define the order of model anonymization. Priority models are
    | processed first to ensure dependencies are handled correctly.
    */
    'priority_models' => [],
];



namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use RamiroEstrella\LaravelDatabaseAnonymize\Traits\Anonymizable;
use Faker\Generator;

class User extends Authenticatable
{
    use Anonymizable;

    public function toAnonymize(Generator $faker): array
    {
        return [
            'email' => $this->id . '@custom.dev',
            'password' => 'secret',
            'firstname' => $faker->firstName,
            'surname' => $faker->lastName,
            'phone' => $faker->e164PhoneNumber,
            'position' => $faker->jobTitle,
            'token' => null,
        ];
    }

    // Optional: Specify conditions for anonymization
    public function anonymizeCondition(): Builder
    {
        return self::withTrashed()->where('something', '>=', '...');
    }
}



namespace App\Models;

use RamiroEstrella\LaravelDatabaseAnonymize\Traits\Anonymizable;
use Faker\Generator;

class Employee extends Authenticatable
{
    use Anonymizable;

    public function anonymizableAttributes(Generator $faker): array
    {
        $firstName = $faker->firstName();
        $lastName = $faker->lastName();

        return [
            'name' => $firstName . ' ' . $lastName,
            'relations' => [
                'person' => [
                    'first_name' => $firstName,
                    'family_name' => $lastName,
                ],
            ],
        ];
    }
}
bash
php artisan vendor:publish --provider="RamiroEstrella\LaravelDatabaseAnonymize\Providers\DatabaseAnonymizeServiceProvider"
bash
php artisan db:anonymize
bash
php artisan db:anonymize --model=App\\\\Models\\\\User --model=App\\\\Models\\\\Employee
bash
php artisan db:anonymize --exclude=App\\\\Models\\\\Employee