PHPackages                             timedoor/laravel-role-js - 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. timedoor/laravel-role-js

ActiveLibrary

timedoor/laravel-role-js
========================

Import role &amp; permission in Laravel into Javascript.

v0.1.2(2y ago)01.8k1[2 PRs](https://github.com/backend-timedoor/laravel-role-js/pulls)1MITPHPPHP ^8.0

Since May 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/backend-timedoor/laravel-role-js)[ Packagist](https://packagist.org/packages/timedoor/laravel-role-js)[ Docs](https://github.com/backend-timedoor/laravel-role-js)[ RSS](/packages/timedoor-laravel-role-js/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (12)Versions (6)Used By (1)

Import role &amp; permission in Laravel into Javascript.
========================================================

[](#import-role--permission-in-laravel-into-javascript)

[![Latest Version on Packagist](https://camo.githubusercontent.com/982badfcc14a8d856621bc605e0bf6b9c5885115292ad003a5809c4b3a43f28a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74696d65646f6f722f6c61726176656c2d726f6c652d6a732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timedoor/laravel-role-js)[![GitHub Tests Action Status](https://camo.githubusercontent.com/9b3ac69080ac4ac0606fab9dac7146818e0a302abab99593e0289dab234029d4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6261636b656e642d74696d65646f6f722f6c61726176656c2d726f6c652d6a732f72756e2d74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/backend-timedoor/laravel-role-js/actions?query=workflow%3Arun-tests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/2e96b70e3a9010349be7b2598c68b538305b70855b8944d8a8f7d68deda3cac2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6261636b656e642d74696d65646f6f722f6c61726176656c2d726f6c652d6a732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d6173746572266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/backend-timedoor/laravel-role-js/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/6acb496f86a7eee3ee449b2a9a5dc8afb144d00cec44d95f2afa1e793207d51c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74696d65646f6f722f6c61726176656c2d726f6c652d6a732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timedoor/laravel-role-js)

Import roles &amp; permissions data from Laravel into Javascript.

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

[](#installation)

You can install the package via composer:

```
composer require timedoor/laravel-role-js
```

Don't forget to install [jeremykenedy/laravel-roles](https://github.com/jeremykenedy/laravel-roles) package to use default setting.

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-role-js-config"
```

This is the contents of the published config file:

```
return [
    'generator' => timedoor\RoleJs\Generator\JeremyKenedyRoleGenerator::class,
];
```

Initialization
--------------

[](#initialization)

First, publish the JavaScript files that contain logic for roles and permissions. The files will publish to `resources/js/roles` path.

```
php artisan role-js:publish
```

if you want to change the publish path, spesify the path in the command.

```
php artisan role-js:publish your/publish/path/roles
```

Second, run the command to generate the JavaScript file contains roles &amp; permissions data. It will generate file `data.ts` in the publish path.

```
php artisan role-js:generate
```

You can also spesify the publish path in the command.

```
php artisan role-js:generate your/publish/path/roles
```

Usage
-----

[](#usage)

You can use the generated file in your JavaScript code.

```
import { HasRolePermission, useRoles } from 'resources/js/roles';

// Example user data with one role
const admin: HasRolePermission = {
    roles: 'admin',
};

const { hasRole, hasPermission } = useRoles(admin);

// Check if user has the given role
if (hasRole('admin')) {
    // Do something
}

// check if user has one of the given roles
if (hasRole(['admin', 'editor'])) {
    // Do something
}

// check if user has a permission
if (hasPermission('edit.users')) {
    // Do something
}

// check if user has one of the given permissions
if (hasPermission(['view.users', 'edit.users'])) {
    // Do something
}

// check if user has all of the given permissions
if (hasPermission(['view.users', 'edit.users'], true)) {
    // Do something
}
```

Custom Generator
----------------

[](#custom-generator)

You can create your own generator by implementing `timedoor\RoleJs\Generator\GeneratorInterface` interface.

```
