PHPackages                             ephort/laravel-data-authorization - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. ephort/laravel-data-authorization

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

ephort/laravel-data-authorization
=================================

Add authorization to your data

v1.0.0(2y ago)54.0k[5 PRs](https://github.com/ephort/laravel-data-authorization/pulls)MITPHPPHP ^8.3CI passing

Since Jun 4Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/ephort/laravel-data-authorization)[ Packagist](https://packagist.org/packages/ephort/laravel-data-authorization)[ Docs](https://github.com/ephort/laravel-data-authorization)[ RSS](/packages/ephort-laravel-data-authorization/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (1)Dependencies (15)Versions (8)Used By (0)

Add authorization to your [`spatie/laravel-data`](https://github.com/spatie/laravel-data/) objects
==================================================================================================

[](#add-authorization-to-your-spatielaravel-data-objects)

[![Latest Version on Packagist](https://camo.githubusercontent.com/de3afc966625b41c0f88272e6074ac96c559b788ba1041fa8d266f6e4147e3fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6570686f72742f6c61726176656c2d646174612d617574686f72697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ephort/laravel-data-authorization)[![GitHub Tests Action Status](https://camo.githubusercontent.com/f14911bc9e740ee969371be76d9f24a680de34e29911a76f8926033de0230305/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6570686f72742f6c61726176656c2d646174612d617574686f72697a6174696f6e2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/ephort/laravel-data-authorization/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/00f23b3dd6d2327b2ba5a33b86ded2d9ba0afec340cb8dc9bc4ea69a70a4464c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6570686f72742f6c61726176656c2d646174612d617574686f72697a6174696f6e2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/ephort/laravel-data-authorization/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/98c8f1f5910aae297dbd846ad8f71732f8e9c466692a608125d14de90467797e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6570686f72742f6c61726176656c2d646174612d617574686f72697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ephort/laravel-data-authorization)

This package adds authorization to your [`spatie/laravel-data`](https://github.com/spatie/laravel-data/) objects, which is very useful if you want to expose data objects to the frontend (e.g. when using Inertia), but still need to check if the user is allowed to perform certain actions.

Installation
------------

[](#installation)

Install the package via composer:

```
composer require ephort/laravel-data-authorization
```

Usage
-----

[](#usage)

This package is intended to be used with [Inertia](https://inertiajs.com/), but does not require it or depend on it.

To add the authorization checks to your data objects, extend the `DataWithAuthorization` class.
All the methods of the base `Data` class are still available.

Next, implement the static `getAuthorizations` method, which should return an array containing the names of the actions that need to be exposed and checked.

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

When the data object is transformed, a lazy `authorization` property is appended to the resulting array.

This property contains a key for each defined policy action and is evaluated by `Gate::allows`.

```
{
    "id": 1,
    "name": "Taylor Otwell",
    "authorization": {
        "view": true,
        "update": false,
        "delete": false
    }
}
```

### Avoid processing authorizations

[](#avoid-processing-authorizations)

Because the `authorization` property is lazy, we can exclude it from the data object to avoid calling the gate on every serialization.

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

Or use the built-in helper method:

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

### Note when using custom `from` methods

[](#note-when-using-custom-from-methods)

When using a [custom `from` method](https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/creating-a-data-object#content-magical-creation), the pipeline that resolves authorizations is not used.

This means you must call the static `resolveAuthorizationArray` method manually when instantiating your data object:

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

You can also wrap the `authorization` array in a Lazy property if needed:

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

TypeScript support
------------------

[](#typescript-support)

Thanks to Spatie, it's very easy to generate TypeScript interfaces from data objects and enums.
Install the [TypeScript Transformer package](https://spatie.be/docs/typescript-transformer) and publish its configuration file:

```
composer require spatie/laravel-typescript-transformer
php artisan vendor:publish --tag=typescript-transformer-config
```

Open `config/typescript-transformer.php` and add the following collector and transformer:

`Ephort\LaravelDataAuthorization\Collectors\DataAuthorizationTypeScriptCollector::class` must be the first collector.

```
'collectors' => [
+   Ephort\LaravelDataAuthorization\Collectors\DataAuthorizationTypeScriptCollector::class,
    Spatie\TypeScriptTransformer\Collectors\DefaultCollector::class,
    Spatie\TypeScriptTransformer\Collectors\EnumCollector::class,
],
```

```
'transformers' => [
    Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer::class,
    Spatie\TypeScriptTransformer\Transformers\EnumTransformer::class,
    Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer::class,
    Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer::class,
+   Ephort\LaravelDataAuthorization\Transformers\DataAuthorizationTypeScriptTransformer::class,
],
```

The above configuration uses a collector provided by this package, which finds data objects that extend `DataWithAuthorization` and generates typings with their authorizations. This is what powers typed authorization support.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

The code is primarily copied from the awesome project [Hybridly](https://github.com/hybridly/hybridly) by [Enzo Innocenzi](https://x.com/enzoinnocenzi), which is a great alternative to [Inertia](https://inertiajs.com/).

- [Peter Brinck](https://github.com/peterbrinck)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance59

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

760d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6154068?v=4)[Peter Brinck](/maintainers/peterbrinck)[@peterbrinck](https://github.com/peterbrinck)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (6 commits)")[![peterbrinck](https://avatars.githubusercontent.com/u/6154068?v=4)](https://github.com/peterbrinck "peterbrinck (2 commits)")

---

Tags

authorizationhybridlyinertiajslaravellaravel-datalaravellaravel-dataephortlaravel-data-authorization

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/ephort-laravel-data-authorization/health.svg)

```
[![Health](https://phpackages.com/badges/ephort-laravel-data-authorization/health.svg)](https://phpackages.com/packages/ephort-laravel-data-authorization)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k102.4M1.4k](/packages/spatie-laravel-permission)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[spatie/laravel-passkeys

Use passkeys in your Laravel app

471890.7k39](/packages/spatie-laravel-passkeys)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

44855.7k](/packages/harris21-laravel-fuse)[danestves/laravel-polar

A package to easily integrate your Laravel application with Polar.sh

8120.4k](/packages/danestves-laravel-polar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
