irabbi360/
laravel-attribute-mask
g4t.io

Mask Sensitive Eloquent Attributes on Retrieval in Laravel

19 views 31 stars by Paul Redmond

Mask Eloquent attributes on retrieval like $casts

README from irabbi360/laravel-attribute-mask Open on GitHub →

Mask Eloquent Attributes on Retrieval

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A Laravel package that automatically masks sensitive model attributes on retrieval. Supports email, phone, and text masking with highly configurable rules.

Features

  • Automatic attribute masking on retrieval
  • Email, phone, and text masking support
  • Configurable mask character and visibility
  • Global or per-attribute masking rules
  • Auto-detection of phone fields by column name

Installation

composer require irabbi360/laravel-attribute-mask

Publish the config file:

php artisan vendor:publish --tag="attribute-mask-config"

Configuration

The default configuration (config/attribute-mask.php):

return [
    'enabled' => true,
    'mask_char' => '*',
    
    'email_masking' => [
        'show_domain' => true,
        'show_start' => 1,
        'show_end' => 1,
    ],
    
    'phone_masking' => [
        'show_start' => 3,
        'show_end' => 2,
        'patterns' => ['phone', 'phone_number', 'mobile', 'mobile_number', ...],
    ],
    
    'text_masking' => [
        'show_start' => 3,
        'show_end' => 3,
    ],
];

Usage

Define Maskable Attributes

Add the HasMaskedAttributes trait and define maskable attributes using the maskables() method:

use Irabbi360\LaravelAttributeMask\Concern\HasMaskedAttributes;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasMaskedAttributes;
    
    /**
     * Get the attributes that should be masked.
     */
    protected function maskables(): array
    {
        return ['email', 'phone', 'phone_number', 'ssn'];
    }
}

Alternatively, use the $maskable property:

class User extends Model
{
    use HasMaskedAttributes;
    
    protected array $maskable = ['email', 'phone', 'ssn'];
}

Masking Behavior

Attributes are automatically masked on retrieval:

$user = User::find(1);

$user->email;       // t**t@example.com
$user->phone;       // 123****90
$user->ssn;         // 123***789

Retrieving Original Values

Get the unmasked value using getOriginal():

$user->getOriginal('email');  // test@example.com

Or temporarily disable masking:

config(['attribute-mask.enabled' => false]);
$original = $user->email;
config(['attribute-mask.enabled' => true]);

Email Masking

Configure email masking behavior:

'email_masking' => [
    'show_domain' => true,      // Show domain part
    'show_start' => 2,          // Show first 2 characters
    'show_end' => 2,            // Show last 2 characters
],

Examples:

  • test@example.com → te**t@example.com
  • john.doe@example.com → jo**oe@example.com

Phone Masking

Phone fields are auto-detected by column name. Configure visibility:

'phone_masking' => [
    'show_start' => 3,
    'show_end' => 2,
],

Examples:

  • 1234567890 → 123****90
  • +1-555-123-4567 → +15-***-67

Add custom phone patterns:

'phone_masking' => [
    'patterns' => ['phone', 'mobile', 'whatsapp', 'fax'],
],

Text Masking

For other text attributes:

'text_masking' => [
    'show_start' => 3,
    'show_end' => 3,
],

Examples:

  • secretpassword → sec********rd
  • API_KEY_12345 → API***345

Custom Mask Character

Change the mask character globally:

'mask_char' => '#',

// Result: test@example.com → t##t@example.com

Disable Masking

Disable globally:

'enabled' => false,

Or temporarily:

config(['attribute-mask.enabled' => false]);
$user->email;  // Returns unmasked value

Testing

composer test

Changelog

See CHANGELOG.md for details on updates.

Contributing

See CONTRIBUTING.md for contribution guidelines.

Security

Report security vulnerabilities via Security Policy.

Credits

License

The MIT License. See LICENSE.md for details.

More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define — masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News