Detect PII in Laravel with Privacy Filter Package | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Privacy Filter: Detect and Redact PII in Text from Laravel        On this page       1. [  Detect and Redact PII in Laravel with Privacy Filter ](#detect-and-redact-pii-in-laravel-with-privacy-filter)
2. [  Classifying Text into Entities ](#classifying-text-into-entities)
3. [  Tuning Confidence Thresholds ](#tuning-confidence-thresholds)
4. [  Redacting Detected Text ](#redacting-detected-text)
5. [  Faking Classifications in Tests ](#faking-classifications-in-tests)
6. [  Installation and Setup ](#installation-and-setup)
7. [  Production Considerations ](#production-considerations)
8. [  Key Takeaways ](#key-takeaways)

  ![Privacy Filter: Detect and Redact PII in Text from Laravel](https://cdn.msaied.com/268/b1cb3d149f11148e853acc85a4a958ae.png)

 [  Laravel ](https://msaied.com/articles?category=laravel) [  Composer Pacakge ](https://msaied.com/articles?category=composer-pacakge)  #Laravel   #PII Detection   #Privacy   #Composer Package   #PHP  

 Privacy Filter: Detect and Redact PII in Text from Laravel 
============================================================

     22 Jun 2026      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   Detect and Redact PII in Laravel with Privacy Filter  ](#detect-and-redact-pii-in-laravel-with-privacy-filter)
2. [  02   Classifying Text into Entities  ](#classifying-text-into-entities)
3. [  03   Tuning Confidence Thresholds  ](#tuning-confidence-thresholds)
4. [  04   Redacting Detected Text  ](#redacting-detected-text)
5. [  05   Faking Classifications in Tests  ](#faking-classifications-in-tests)
6. [  06   Installation and Setup  ](#installation-and-setup)
7. [  07   Production Considerations  ](#production-considerations)
8. [  08   Key Takeaways  ](#key-takeaways)

 Detect and Redact PII in Laravel with Privacy Filter
----------------------------------------------------

Handling user data responsibly is a core requirement for modern web applications. The [Privacy Filter](https://github.com/DirectoryTree/PrivacyFilter) package for Laravel makes it straightforward to detect personally identifiable information (PII) — such as names and email addresses — directly within your PHP application, without sending data to a third-party API.

Under the hood, the package wraps `privacy-filter.cpp`, a GGML inference engine for OpenAI's privacy-filter token-classification models, and exposes the results through a clean Laravel facade.

Classifying Text into Entities
------------------------------

The primary entry point is the `entities()` method. Pass it a string and it returns a collection of `Entity` instances, each carrying the entity type, matched text, UTF-8 byte offsets, and a confidence score.

```php
use DirectoryTree\PrivacyFilter\Facades\PrivacyFilter;

$entities = PrivacyFilter::entities('Contact John Doe at jdoe@example.com.');

foreach ($entities as $entity) {
    echo $entity->type; // private_email
    echo $entity->text; // jdoe@example.com
}

```

Because offsets are exact UTF-8 byte positions, you can map each detected entity back to its precise location in the original string — no fragile string-matching required.

Tuning Confidence Thresholds
----------------------------

Classifications default to a `0.5` confidence threshold. You can raise or lower it per call to trade recall for precision. A higher threshold returns fewer but more certain results.

```php
$entities = PrivacyFilter::entities(
    text: 'Contact John Doe at jdoe@example.com.',
    threshold: 0.75,
);

```

Redacting Detected Text
-----------------------

Privacy Filter focuses on detection; redaction is left to you. A simple `reduce` over the returned entities handles the substitution:

```php
$redacted = collect($entities)->reduce(function (string $text, $entity) {
    return str_replace($entity->text, '[redacted]', $text);
}, $text);

```

This keeps the package's responsibilities narrow and your redaction logic fully under your control.

Faking Classifications in Tests
-------------------------------

Loading a GGML model in a test suite is slow. Privacy Filter ships a `fake()` method that returns predetermined entities without invoking the binary at all:

```php
use DirectoryTree\PrivacyFilter\Entity;
use DirectoryTree\PrivacyFilter\Facades\PrivacyFilter;

PrivacyFilter::fake([
    new Entity(type: 'private_email', start: 20, end: 36, score: 0.98),
]);

```

Your application code under test receives those entities exactly as if the model had produced them, keeping your test suite fast and deterministic.

Installation and Setup
----------------------

Install via Composer, then run the built-in installer to fetch the compiled binary and download the required GGUF model:

```bash
composer require directorytree/privacy-filter
php artisan privacy-filter:install

```

Prebuilt binaries are available for Linux, macOS (including ARM64), and Windows. Pass `--force` to overwrite existing files. To customize the binary path, model path, process timeout, model URL, or release source, publish the config file:

```bash
php artisan vendor:publish --tag=privacy-filter-config

```

### Production Considerations

Every classification loads the model into memory. The package recommends running classifications on **dedicated queue workers** in production to keep memory usage predictable and avoid impacting your web processes.

Key Takeaways
-------------

- Detects PII (names, emails, and more) locally using a GGML model — no external API calls.
- Returns `Entity` objects with type, text, UTF-8 byte offsets, and confidence score.
- Adjustable confidence threshold per call for precision vs. recall trade-offs.
- Redaction is intentionally left to the developer for maximum flexibility.
- `fake()` method enables fast, deterministic unit tests without loading the model.
- Prebuilt binaries support Linux, macOS (ARM64 included), and Windows.
- Run classifications on queue workers in production to manage memory predictably.

---

*Source: [Privacy Filter: Detect PII in Text from Laravel — Laravel News](https://laravel-news.com/privacy-filter-detect-pii-in-text-from-laravel)*

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fprivacy-filter-detect-and-redact-pii-in-text-from-laravel&text=Privacy+Filter%3A+Detect+and+Redact+PII+in+Text+from+Laravel) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fprivacy-filter-detect-and-redact-pii-in-text-from-laravel) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  Does Privacy Filter send text data to an external API?        No. Privacy Filter runs a local GGML inference engine (privacy-filter.cpp) on your own server, so text never leaves your infrastructure. 

      Q02  How do I avoid slow model loading in my test suite?        Use the PrivacyFilter::fake() method to supply predetermined Entity objects. Your application code receives them as if the model produced them, with no binary or model loaded. 

      Q03  What is the recommended way to run Privacy Filter in production?        The package recommends running classifications on dedicated queue workers, because every classification loads the GGML model into memory. This keeps memory usage predictable and isolates the workload from your web processes. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Showcase Your PhpStorm Expertise on LinkedIn with JetBrains' New Plugin](https://cdn.msaied.com/267/a94d1b197b4892a531075bc5ecda0ac2.png) PhpStorm JetBrains LinkedIn 

### Showcase Your PhpStorm Expertise on LinkedIn with JetBrains' New Plugin

JetBrains has launched a free LinkedIn Connected Apps plugin for PhpStorm and other JetBrains IDEs. It tracks...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 22 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/showcase-your-phpstorm-expertise-on-linkedin-with-jetbrains-new-plugin) [ ![Laravel Enums as First-Class Domain Citizens: Typed Casts, Backed Values, and Behaviour](https://cdn.msaied.com/266/81a05f630d54004d2a6689a02d6f0579.png) laravel php enums 

### Laravel Enums as First-Class Domain Citizens: Typed Casts, Backed Values, and Behaviour

PHP 8.1 backed enums are more than constants. Learn how to attach behaviour, use them as Eloquent casts, embed...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 22 Jun 2026     1 min read  

  Read    

 ](https://msaied.com/articles/laravel-enums-as-first-class-domain-citizens-typed-casts-backed-values-and-behaviour) [ ![MySQL EXPLAIN and Index Optimization for Laravel Developers](https://cdn.msaied.com/265/e97881aef9580e1f0b9e1bd6890d828a.png) laravel mysql performance 

### MySQL EXPLAIN and Index Optimization for Laravel Developers

Stop guessing why your Laravel queries are slow. Learn to read EXPLAIN output, spot full table scans, and desi...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 22 Jun 2026     4 min read  

  Read    

 ](https://msaied.com/articles/mysql-explain-and-index-optimization-for-laravel-developers) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
