PHP code example of ephort / laravel-data-authorization

1. Go to this page and download the library: Download ephort/laravel-data-authorization 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/ */

    

ephort / laravel-data-authorization example snippets


use Ephort\LaravelDataAuthorization\DataWithAuthorization;

class UserData extends DataWithAuthorization
{
    public function __construct(
        public int $id,
        public string $name,
    ) {
    }
    
    public static function getAuthorizations(): array
    {
        return [
            'view',
            'update',
            'delete',
        ];
    }
}

UserData::from($user)->exclude('authorization');

UserData::from($user)->withoutAuthorization();

public static function fromModel(User $user): self
{
    return self::from([
        'id' => $user->id,
        'name' => $user->name,
        'authorization' => static::resolveAuthorizationArray($user),
    ]);
}

Lazy::create(fn () => static::resolveAuthorizationArray($user))->defaultIncluded();