Search by

jeffersongoncalves / laravel-lgpd

jeffersongoncalves

LGPD toolkit core for Laravel: declare personal data on Eloquent models, build the data inventory, export and anonymize data subjects.

1.2.0 2026-11-17 01:40 UTC

This package is auto-updated.

Last update: 2026-11-17 01:40:31 UTC


README

Laravel LGPD

Latest Version on Packagist Tests Total Downloads

Core of a toolkit for the Brazilian General Data Protection Law (LGPD, Lei 13.709/2018) in Laravel. You declare which columns of your Eloquent models hold personal data. With those declarations the package can:

  • build the personal data inventory (data mapping that feeds the ROPA, art. 37);
  • export a record's personal data (data subject access and portability, art. 18, II/V);
  • anonymize a record, choosing a strategy per column (art. 18, IV/VI).

This package gives you tooling. It does not make you compliant on its own. You still need legal review for legal bases, retention periods and your privacy notice.

Installation

composer require jeffersongoncalves/laravel-lgpd
php artisan vendor:publish --tag="lgpd-config"

Declaring personal data

use JeffersonGoncalves\Lgpd\Attributes\PersonalData;
use JeffersonGoncalves\Lgpd\Concerns\InteractsWithPersonalData;
use JeffersonGoncalves\Lgpd\Contracts\HasPersonalData;
use JeffersonGoncalves\Lgpd\Enums\{AnonymizeStrategy, DataCategory, LegalBasis};

#[PersonalData('name', purpose: 'Account identification', legalBasis: LegalBasis::Contract, anonymize: AnonymizeStrategy::Mask)]
#[PersonalData('email', legalBasis: LegalBasis::Contract, retention: '5 years', anonymize: AnonymizeStrategy::Hash)]
#[PersonalData('health_notes', category: DataCategory::Sensitive, legalBasis: LegalBasis::Consent)]
#[PersonalData('tax_id', legalBasis: LegalBasis::LegalObligation, anonymize: AnonymizeStrategy::Keep)]
class User extends Authenticatable implements HasPersonalData
{
    use InteractsWithPersonalData;
}

Register the model in config/lgpd.php:

'models' => [
    App\Models\User::class,
],

Anonymize strategies

Strategy Result
Nullify (default) null
Hash HMAC-SHA256 keyed by LGPD_HASH_KEY (falls back to APP_KEY). It gives a stable pseudonym, so unique indexes keep working
Mask Maria Silva → M**********
Keep untouched, for data you must keep by law (e.g. tax records)

Encrypted casts ('encrypted') work as expected: the value is decrypted, then anonymized, then encrypted again.

Usage

use JeffersonGoncalves\Lgpd\Facades\Lgpd;

Lgpd::inventory();                 // one row per declared column
$user->exportPersonalData();       // ['name' => ..., 'email' => ..., ...]
$user->anonymizePersonalData();    // applies each strategy and saves

anonymizePersonalData() fires JeffersonGoncalves\Lgpd\Events\PersonalDataAnonymized with the model and the columns it changed. Columns marked Keep are not in that list. laravel-lgpd-retention listens to it to write tombstones. With those, it can erase again any data that a backup restore brings back.

php artisan lgpd:inventory          # table
php artisan lgpd:inventory --json   # machine readable

k-anonymity

Before you share or publish a dataset as anonymized (art. 12), check that nobody can be singled out by combining quasi-identifiers such as ZIP code, birth date and gender. A dataset is k-anonymous when every combination of those values is shared by at least k rows. It is l-diverse when each of those groups also holds at least l distinct sensitive values.

use JeffersonGoncalves\Lgpd\Facades\Lgpd;
use JeffersonGoncalves\Lgpd\Generalize;

Lgpd::kAnonymity(Patient::class, [
    'zip' => Generalize::prefix(5),         // 01310-100 → 01310
    'birth_date' => Generalize::date('Y'),  // 1990-03-01 → 1990
    'age' => Generalize::range(10),         // 34 → 30-39
    'gender',                               // as stored
], k: 5, sensitive: 'diagnosis');

// ['rows' => 1200, 'groups' => 87, 'k' => 3, 'rows_below_k' => 14, 'l' => 1]

k is the smallest group, rows_below_k counts the rows you still need to generalize further or suppress, and l is the smallest number of distinct sensitive values in a group. It accepts a model class or a query builder. Values are read through the model, so casts apply. The generalizers are plain closures, so you can also use them to build the dataset you export.

php artisan lgpd:k-anonymity "App\Models\Patient" zip birth_date --k=5 --sensitive=diagnosis

The command fails (exit code 1) when the data is below the target k, so it can run in CI or before a scheduled export. It scans every row, so run it off-peak on big tables.

Testing

composer test

Changelog

See CHANGELOG.

License

The MIT License (MIT). See License File.