PHP code example of irabbi360 / laravel-attribute-mask

1. Go to this page and download the library: Download irabbi360/laravel-attribute-mask 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/ */

    

irabbi360 / laravel-attribute-mask example snippets


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,
    ],
];

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'];
    }
}

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

$user = User::find(1);

$user->email;       // t**[email protected]
$user->phone;       // 123****90
$user->ssn;         // 123***789

$user->getOriginal('email');  // [email protected]

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

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

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

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

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

'mask_char' => '#',

// Result: [email protected] → t##[email protected]

'enabled' => false,

config(['attribute-mask.enabled' => false]);
$user->email;  // Returns unmasked value
bash
php artisan vendor:publish --tag="attribute-mask-config"