PHP code example of graystackit / laravel-gdpr-compliance
1. Go to this page and download the library: Download graystackit/laravel-gdpr-compliance 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/ */
graystackit / laravel-gdpr-compliance example snippets
namespace App\Models;
use GraystackIt\Gdpr\Contracts\PersonalData;
use GraystackIt\Gdpr\Enums\RetentionMode;
use GraystackIt\Gdpr\Support\PersonalDataBlueprint;
use GraystackIt\Gdpr\Traits\HasConsentRecords;
use GraystackIt\Gdpr\Traits\HasPersonalData;
use GraystackIt\Gdpr\Traits\IsPersonalDataSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements PersonalData
{
use HasPersonalData, IsPersonalDataSubject, HasConsentRecords;
public function personalData(PersonalDataBlueprint $b): PersonalDataBlueprint
{
return $b
// PII: anonymize AND export
->field('name')->anonymizeWith('name')->exportable()
->field('email')->anonymizeWith('email')->exportable()
->field('phone')->anonymizeWith('phone')->exportable()
// PII internal: anonymize only, do NOT export
->field('password')
->anonymizeWith('static_text', ['value' => '[ANONYMIZED]'])
// Non-PII metadata: export only, never touched
->field('created_at')->exportable()
->field('locale')->exportable()
->retention(
mode: RetentionMode::Delete,
gracePeriodDays: 7, // 0 = immediate, max 30
)
->processOrder(1000); // subject is processed last
}
}
namespace App\Models;
use GraystackIt\Gdpr\Contracts\PersonalData;
use GraystackIt\Gdpr\Enums\RetentionMode;
use GraystackIt\Gdpr\Support\PersonalDataBlueprint;
use GraystackIt\Gdpr\Traits\HasPersonalData;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Order extends Model implements PersonalData
{
use HasPersonalData;
public function personalData(PersonalDataBlueprint $b): PersonalDataBlueprint
{
return $b
->field('shipping_address')->anonymizeWith('address')->exportable()
->field('billing_email')->anonymizeWith('email')->exportable()
->field('total')->exportable()
->field('created_at')->exportable()
->retention(
mode: RetentionMode::LegalHold,
legalHoldDays: 3650, // 10 years
legalBasis: '§ 147 AO — tax record retention',
)
->processOrder(100); // children before subject
}
public function scopePersonalDataForSubject(Builder $query, Model $subject): Builder
{
return match (true) {
$subject instanceof \App\Models\User => $query->where('user_id', $subject->getKey()),
default => $query->whereRaw('1 = 0'),
};
}
}
// In your auth logic
if (GDPR::isDeletionPending($user)) {
// Block login, show banner, redirect, etc.
}
// Or as middleware on auth routes
Route::middleware('gdpr.no-deletion-pending')->group(function () {
// ...
});
// Or as a query scope
User::whereNotDeletionPending()->where('email', $email)->first();
// Block routes that dpr.consent:marketing')->group(function () {
// Returns 451 Unavailable For Legal Reasons if consent is missing
});
// Necessary always passes
Route::middleware('gdpr.consent:necessary')->group(function () {
// Always accessible
});
use GraystackIt\Gdpr\Events\PersonalDataErased;
Event::listen(PersonalDataErased::class, function ($event) {
// $event->deletion->subject_type, $event->deletion->subject_id
// Clean up Stripe, Mailchimp, S3 avatars, etc.
});
use Illuminate\Support\Facades\Schedule;
Schedule::command('gdpr:process-deletions')->daily();
Schedule::command('gdpr:cleanup-exports')->daily();
Schedule::command('gdpr:prune')->weekly();