PHPackages                             saeidsharafi/laravel-permission-generator - 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. saeidsharafi/laravel-permission-generator

ActiveLaravel-package[Authentication &amp; Authorization](/categories/authentication)

saeidsharafi/laravel-permission-generator
=========================================

Generate Permission Enums and sync with spatie/laravel-permission based on a config file.

v1.1.2(12mo ago)0168MITPHPPHP ^8.1

Since Mar 29Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/SaeidSharafi/laravel-permission-generator)[ Packagist](https://packagist.org/packages/saeidsharafi/laravel-permission-generator)[ RSS](/packages/saeidsharafi-laravel-permission-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (8)Versions (5)Used By (0)

Laravel Permission Generator
============================

[](#laravel-permission-generator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5b7949a5edf680ffafdb98ce24216541e775de8894603cec2d068565ef22db99/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7361656964736861726166692f6c61726176656c2d7065726d697373696f6e2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/saeidsharafi/laravel-permission-generator)[![Total Downloads](https://camo.githubusercontent.com/4584656ed64bae24e6ac5b9a9b3b0a6ee1089551d506c89b22065f3531e6a5d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7361656964736861726166692f6c61726176656c2d7065726d697373696f6e2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/saeidsharafi/laravel-permission-generator)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Generate a PHP Permission Enum class for your Laravel application based on a configuration file and keep it synchronized with your `spatie/laravel-permission` database tables.

Stop manually defining permission strings everywhere and prevent typos!

Features
--------

[](#features)

- Define permission structure (resources, actions, custom permissions) in a single config file.
- Generate a PHP 8.1+ backed Enum class containing all your permissions.
- Provides IDE auto-completion and type safety when referencing permissions.
- Includes an Artisan command to sync the generated permissions with your database (using `spatie/laravel-permission`).
- Handles common permission patterns (e.g., `view_scoped` string generates `resource.view_any` and `resource.view`).
- Supports custom actions and standalone permissions defined via strings or custom Enums.
- Optionally syncs all permissions to a designated "Super Admin" role.
- Optionally cleans up stale permissions from the database.
- Customizable enum template through published stubs.
- Automatically generates the enum file if missing during permissions sync.
- Configurable enum class namespace.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Laravel &gt;= 9.0
- `spatie/laravel-permission` &gt;= 5.5

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

[](#installation)

Install the package via Composer:

```
composer require saeidsharafi/laravel-permission-generator
```

Setup
-----

[](#setup)

1. **Publish the configuration file:**

    ```
    php artisan vendor:publish --provider="SaeidSharafi\LaravelPermissionGenerator\PermissionGeneratorServiceProvider" --tag="permission-generator-config"
    ```

    This creates `config/permission-generator.php` in your application.
2. **Customize the Configuration:**Open `config/permission-generator.php` and define:

    - `output_enum`: The path where your `PermissionEnum.php` file will be created (e.g., `app_path('Enums/PermissionEnum.php')`).
    - `enum_class`: The fully qualified class name of your enum (e.g., `App\Enums\PermissionEnum`).
    - `resources`: List your application resources (e.g., 'user', 'post') and their associated actions. Define actions using:
        - **Strings (Recommended):** Use simple strings like `'view_scoped'`, `'create'`, `'update_scoped'`, or custom action names like `'publish'`, `'manage_roles'`. The generator recognizes specific string values like `'view_scoped'` to automatically create `_any` and simple/`_own` versions. Other strings generate literal `resource.action` permissions.
        - **`PermissionAction` Enum (Optional):** For standard patterns recognized by the generator, you can optionally `use SaeidSharafi\LaravelPermissionGenerator\Enums\PermissionAction;` and use constants like `PermissionAction::VIEW_SCOPED` for clarity.
        - **Your Own Backed Enums (Advanced):** You can use constants from your application's own `BackedEnum` classes (e.g., `App\Enums\MyActions::APPROVE`). The generator will use the Enum case's value. **Important:** Special logic (like `_scoped` generating two permissions) is only triggered by specific *string values* recognized by the package (like `'view_scoped'`), not by the structure of your custom Enum.
    - `custom_permissions`: (Optional) Define standalone permissions not tied to a resource.
    - `super_admin_role`: (Optional) Name of the role to grant all permissions.
    - `remove_stale_permissions`: (Optional) Set to `true` to enable cleanup of old permissions during sync (use with caution).
3. **Customize Enum Templates (Optional):**

    ```
    php artisan vendor:publish --provider="SaeidSharafi\LaravelPermissionGenerator\PermissionGeneratorServiceProvider" --tag="permission-generator-stubs"
    ```

    This publishes the enum template to `stubs/vendor/permission-generator/enum.stub` where you can customize it.

Usage Workflow
--------------

[](#usage-workflow)

1. **Configure:** Define your desired permission structure in `config/permission-generator.php`.
2. **Generate Enum:** Create or update your `PermissionEnum.php` file:

    ```
    php artisan permissions:generate-enum
    ```

    - Use `--force` to overwrite without confirmation.
3. **Sync Database:** Ensure the permissions defined in your Enum exist in the database for `spatie/laravel-permission`:

    ```
    php artisan permissions:sync
    ```

    - Use `--fresh` **with extreme caution** to delete *all* existing permissions and their assignments before syncing.
    - Use `--yes` or `-Y` to skip confirmation prompts.
4. **Use the Enum:** Import and use your generated Enum (e.g., `App\Enums\PermissionEnum`) in your code (Policies, Middleware, Controllers, Filament, etc.) for type safety and auto-completion.

    ```
    use App\Enums\PermissionEnum; // Adjust namespace if you changed the output path

    // Example Policy
    public function updateAny(User $user): bool
    {
        return $user->hasPermissionTo(PermissionEnum::POST_UPDATE_ANY->value);
    }

    // Example Middleware or Controller Check
    if (! Auth::user()?->can(PermissionEnum::ACCESS_ADMIN_DASHBOARD->value)) {
        abort(403);
    }
    ```

Streamlined Workflow
--------------------

[](#streamlined-workflow)

The package now supports a more streamlined workflow:

1. If you run `permissions:sync` without first generating the enum file, it will automatically run `permissions:generate-enum` for you.
2. This makes it easier to get started with minimal setup - just configure your permissions and run `php artisan permissions:sync`.

Configuration Details
---------------------

[](#configuration-details)

See the comments within the published `config/permission-generator.php` file for detailed explanations of each option and how to define resources and actions using strings or Enums.

Development (Linking Local Package)
-----------------------------------

[](#development-linking-local-package)

If you want to contribute or modify the package locally:

1. Clone the package repository separately.
2. In your main Laravel project's `composer.json`, add a `repositories` section: ```
    "repositories": [
        {
            "type": "path",
            "url": "../path/to/your/local/laravel-permission-generator"
        }
    ],
    ```
3. Require the package with `@dev` stability in your project's `composer.json`: ```
    "require": {
        "saeidsharafi/laravel-permission-generator": "@dev",
        // ... other requires ...
    }
    ```
4. Run `composer update saeidsharafi/laravel-permission-generator`.

License
-------

[](#license)

The MIT License (MIT). Please see the [LICENSE](LICENSE) file for more information.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance50

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

Every ~16 days

Total

4

Last Release

360d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/70821e746e348da6d87c64389f6a1e5babb1015a25d50f2bc5b93a87a7a002c3?d=identicon)[SaeidSharafi](/maintainers/SaeidSharafi)

---

Top Contributors

[![SaeidSharafi](https://avatars.githubusercontent.com/u/6916349?v=4)](https://github.com/SaeidSharafi "SaeidSharafi (4 commits)")

---

Tags

spatielaravelenumgeneratorpermission

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/saeidsharafi-laravel-permission-generator/health.svg)

```
[![Health](https://phpackages.com/badges/saeidsharafi-laravel-permission-generator/health.svg)](https://phpackages.com/packages/saeidsharafi-laravel-permission-generator)
```

###  Alternatives

[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[tightenco/jigsaw

Simple static sites with Laravel's Blade.

2.2k438.5k29](/packages/tightenco-jigsaw)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[casbin/laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.

324339.9k4](/packages/casbin-laravel-authz)[codebot/entrust

This package provides a flexible way to add Role-based Permissions to Laravel

1596.6k](/packages/codebot-entrust)

PHPackages © 2026

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