Sooner or later an e-mail arrives: "Please send me all the data you have about me" or "Please delete my account and everything about me". For a Laravel application the first answer is usually a developer running SQL queries by hand, table by table, hoping not to miss the guest orders, the contact form messages or the newsletter list. This guide shows how to make those requests a routine: a map of where personal data lives, a search that finds a person everywhere, a copy they can open, an erasure that keeps what the law makes you keep, and a log with deadlines. It is a technical guide, not legal advice; what you must keep, for how long and whether a request has to be granted are questions for your lawyer or data protection officer. Every code sample was run on Laravel 13.
#What the requests are
The GDPR gives people several rights over their data. The two that reach developers most often are access (article 15: confirmation, a copy of the data and information about its use) and erasure (article 17). Portability (article 20) asks for the data in a structured, machine-readable format. Article 12 sets the timing: an answer without undue delay and at the latest within one month of receiving the request, which can be extended by two further months for complex or numerous requests if the person is told within the first month. The same article lets you ask for information to confirm who the person is when you have doubts, and you should: sending someone else's order history to whoever asked for it is a data breach of its own.
#Start with a map of personal data
You cannot answer a request about data you cannot find. Write down, once, every place where personal data lives, how a record points to a person, what a copy contains and what erasure does with it:
<?php
// Where personal data lives. The account goes last: other rows point to it.
return [
'messages' => [
'model' => App\Models\ContactMessage::class,
'match' => ['email' => 'email'],
'export' => ['name', 'email', 'message', 'created_at'],
'erase' => 'delete',
],
'orders' => [
'model' => App\Models\Order::class,
'match' => ['email' => 'email', 'user_id' => 'account'], // guest and account orders
'export' => ['number', 'total', 'shipping_address', 'phone', 'created_at'],
'erase' => 'keep',
'reason' => 'Invoices are kept for the period accounting law requires.',
],
'account' => [
'model' => App\Models\User::class,
'match' => ['email' => 'email'],
'export' => ['name', 'email', 'phone', 'created_at'],
'erase' => 'anonymize',
'anonymize' => ['name' => 'Deleted customer', 'email' => 'deleted-{id}@deleted.invalid', 'phone' => null],
],
];
The map is also documentation. A new developer sees where personal data is, and adding a table without adding it here becomes a review question. Keep it in a config file, not in closures, so it works with config:cache.
#Find a person everywhere
Two details decide whether the search is complete. People type their address in different ways (Ana@Example.com, ana@example.com, with a space at the end), so compare in lower case and trimmed. And records point to a person in different ways: a guest order carries an e-mail, an order placed while logged in may carry only the account id.
<?php
use App\Models\User;
function find_personal_data(array $map, string $email): array
{
$email = mb_strtolower(trim($email));
$account = User::query()->whereRaw('LOWER(TRIM(email)) = ?', [$email])->first();
$found = [];
foreach ($map as $key => $source) {
// Without an account, "account" matches are skipped; an empty group would match every row.
$match = array_filter($source['match'], fn ($by) => $by === 'email' || $account !== null);
if ($match === []) {
$found[$key] = collect();
continue;
}
$found[$key] = $source['model']::query()->where(function ($q) use ($match, $email, $account) {
foreach ($match as $column => $by) {
$by === 'email'
? $q->orWhereRaw('LOWER(TRIM('.$q->getGrammar()->wrap($column).')) = ?', [$email])
: $q->orWhere($column, $account->getKey());
}
})->get();
}
return $found;
}
The guard in the middle matters more than it looks. In Laravel, a where() group that ends up empty adds no condition at all: in our test, an empty group matched 4 of 4 orders. For a search it would return other people's data; for an erasure it would delete them. Make "no condition" mean "no rows", explicitly.
In our test, a request typed as " ana@EXAMPLE.com" found her contact message (stored with capitals and a trailing space), both her account order and her guest order, and her account. A guest without an account was found through guest orders only.
#A copy people can open
The copy goes to a person, not to a developer, so it should open in ordinary software: a JSON file for completeness and portability, one CSV file per source for reading, and a short README that explains what each file is. CSV has one trap. Spreadsheet programs run a cell that starts with =, +, - or @ as a formula, and a contact form message is text anyone could have typed. OWASP calls this CSV injection; the usual defence is to prefix such cells with an apostrophe so they open as text:
<?php
function csv_cell(mixed $value): string
{
$value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : (string) $value;
// A cell that starts like a formula is opened as text, not run by the spreadsheet.
return preg_match('/^[=+\-@\t\r]/', $value) ? "'".$value : $value;
}
function csv_rows(array $rows, array $columns): string
{
$out = fopen('php://temp', 'w+');
fwrite($out, "\xEF\xBB\xBF"); // BOM, so Excel reads UTF-8 (Cyrillic) correctly
fputcsv($out, $columns, ';', '"', '');
foreach ($rows as $row) {
fputcsv($out, array_map(fn ($c) => csv_cell($row[$c] ?? ''), $columns), ';', '"', '');
}
rewind($out);
return stream_get_contents($out);
}
In our run a message reading =HYPERLINK("http://x") came out as text. The price is that -5 and a phone number such as +359 888 also get the apostrophe; for a copy meant for reading that is acceptable. Semicolons as separators and the byte order mark make the file open correctly in Excel with Bulgarian regional settings.
Do not attach the ZIP to an e-mail. Store it on a private disk and send a link that works only for a limited time:
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
// routes/web.php: the file is served only through a valid, unexpired signature.
Route::get('/privacy/download/{file}', function (string $file) {
abort_unless(preg_match('/^[a-z0-9-]+\.zip$/', $file) === 1, 404);
return Storage::disk('local')->download('privacy-exports/'.$file);
})->middleware('signed')->name('privacy.download');
// After writing the ZIP:
function download_link(string $file): string
{
return URL::temporarySignedRoute('privacy.download', now()->addDays(7), ['file' => $file]);
}
We checked it: the link answers, a link with one letter of the file name changed is refused with 403, and the same link eight days later is refused too. Give the file a random name and delete expired files on a schedule.
#Erase what you may, keep what you must
Erasure is not "delete the user". Some data must stay: article 17 itself makes exceptions, among them a legal obligation to keep data, and accounting and tax law in particular require invoices and their details to be kept for years. Other data can be anonymised so the statistics stay intact, and the rest can go. The map says which is which; the code runs a dry run first and does the real thing in one transaction:
<?php
use Illuminate\Support\Facades\DB;
function erase_personal_data(array $map, string $email, bool $dryRun = true): array
{
$report = [];
$run = function () use ($map, $email, $dryRun, &$report) {
foreach (find_personal_data($map, $email) as $key => $records) {
$action = $map[$key]['erase'];
foreach ($records as $record) {
if ($dryRun || $action === 'keep') {
continue;
}
if ($action === 'delete') {
$record->delete();
} else {
$values = array_map(fn ($v) => is_string($v) ? str_replace('{id}', (string) $record->getKey(), $v) : $v, $map[$key]['anonymize']);
$record->forceFill($values)->save();
}
}
$report[$key] = $action.': '.$records->count();
}
};
$dryRun ? $run() : DB::transaction($run);
return $report;
}
The dry run reported "delete: 1, keep: 2, anonymize: 1" and changed nothing; the real run deleted the message, left both orders and turned the account into "Deleted customer" with an address that cannot receive mail. The transaction is not decoration: when we broke the anonymisation on purpose, the message deleted a moment earlier came back, so a failure never leaves a person half erased. Show the dry run to whoever confirms the erasure, so they see what will be kept and why before anything changes.
#Keep a log with deadlines
Record each request: who asked, what kind of request, when it arrived, the deadline, what was done and by whom. A month is a calendar month, not 30 days: from 31 January 2027, one calendar month ends on 28 February, while 30 days would give 2 March. Carbon's addMonthNoOverflow() gives the calendar date; how a deadline is counted in a particular case is a legal question, so treat the date as your reminder. After an erasure is finished, keep only a masked e-mail in the log, such as a***@e***.com, so the log itself does not keep the data you just removed. Retention periods belong in the same map: contact messages older than two years, for example, can be deleted by a scheduled command.
For the rest of a small site's privacy basics, cookies and consent, see our guide to GDPR and cookies.
#A package that does this
We built this into Zabrava, a Laravel package with a Filament panel: one map of personal data in config, search by e-mail through accounts and guest records, a ZIP with JSON, CSV and a README in English and Bulgarian behind a signed link, erasure with a dry run in one transaction, retention periods, and a request log sorted by deadline with late requests marked. It was tested with 19 tests on Laravel 13 with Filament 5 and on Laravel 12 with Filament 4, and is listed as Coming soon. It answers requests; deciding what to keep remains yours.
#Frequently asked questions
How long do I have to answer a GDPR request?
Article 12(3) says without undue delay and at the latest within one month of receipt, extendable by two further months for complex or numerous requests if you tell the person within the first month.
Do I have to delete invoices when a customer asks for erasure?
Usually not: data you are legally obliged to keep is an exception in article 17. Keep those records, erase or anonymise the rest, and tell the person what you kept and why. Ask your accountant for the periods that apply.
Should I send the copy as an e-mail attachment?
Better not. Send a link that expires, to the address you have verified, and delete the file after it expires.
How do I know the person asking is really the customer?
Article 12(6) lets you ask for information to confirm identity when you have reasonable doubts. A request from the address on the account, confirmed through a link, is a common minimum.
Comments
Comments
No comments yet. Ask a question or share what worked for you.
Leave a comment