PHP code example of enriko / laravel-attribute-restrictor

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

    

enriko / laravel-attribute-restrictor example snippets


use Enriko\LaravelAttributeRestrictor\Traits\RestrictedAttributes;

class User extends Authenticatable
{
    use RestrictedAttributes;
    // ...
}

public static function getRestrictedAttributes(): array
{
    return [
        'name',
        'email'
    ];
}

public static function getAttributeRestrictions($attr): callable|bool
{
    return match ($attr) {
        'name' => fn() => Auth::user()->can('see_names'), // varies by user
        'email' => false, // always restricts
        default => true // doesn't restrict
    };
}

public static function getAttributeRestrictionsArray(): array
{
    return [
        'name' => fn() => Auth::user()->can('see_names'), // varies by user
        'email' => false, // always restricts
    ];
}

public static function getGlobalRestriction() : callable|bool {
    return Auth::user()->can('see_sensitive_data');
}

private function getReplacedText()
{
    return 'You can't see user information';
}

// $User = 'Foo'
$User->getRawOriginal('account_number'); // will return 12345, no matter what
bash
php artisan vendor:publish --tag=config