{"id":45204768,"url":"https://github.com/codinglabsau/laravel-roles","last_synced_at":"2026-02-20T15:02:44.621Z","repository":{"id":49096367,"uuid":"248499545","full_name":"codinglabsau/laravel-roles","owner":"codinglabsau","description":"A super simple Laravel roles package","archived":false,"fork":false,"pushed_at":"2025-09-26T01:19:20.000Z","size":35,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2026-01-31T19:18:49.554Z","etag":null,"topics":["laravel","roles","roles-permissions-management"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codinglabsau.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-03-19T12:38:51.000Z","updated_at":"2025-09-30T00:18:18.000Z","dependencies_parsed_at":"2024-03-11T05:29:23.390Z","dependency_job_id":"1dd5cccf-f2db-4d1e-8851-55b8d298915d","html_url":"https://github.com/codinglabsau/laravel-roles","commit_stats":{"total_commits":29,"total_committers":3,"mean_commits":9.666666666666666,"dds":"0.13793103448275867","last_synced_commit":"b6f415ff44bb42600f39557edcd0b29fbaf7bbf4"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/codinglabsau/laravel-roles","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codinglabsau%2Flaravel-roles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codinglabsau%2Flaravel-roles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codinglabsau%2Flaravel-roles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codinglabsau%2Flaravel-roles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codinglabsau","download_url":"https://codeload.github.com/codinglabsau/laravel-roles/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codinglabsau%2Flaravel-roles/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29655026,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-20T09:27:29.698Z","status":"ssl_error","status_checked_at":"2026-02-20T09:26:12.373Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","roles","roles-permissions-management"],"created_at":"2026-02-20T15:02:43.384Z","updated_at":"2026-02-20T15:02:44.611Z","avatar_url":"https://github.com/codinglabsau.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Roles\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/codinglabsau/laravel-roles.svg?style=flat-square)](https://packagist.org/packages/codinglabsau/laravel-roles)\n[![Test](https://github.com/codinglabsau/laravel-roles/actions/workflows/run-tests.yml/badge.svg)](https://github.com/codinglabsau/laravel-roles/actions/workflows/run-tests.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/codinglabsau/laravel-roles.svg?style=flat-square)](https://packagist.org/packages/codinglabsau/laravel-roles)\n\nA simple, flexible roles implementation for Laravel. \n\nSee [v2.3 for Laravel 6-9 support](https://github.com/codinglabsau/laravel-roles/releases/tag/v2.3.0).\n\n## Installation\n### Install with composer\n```bash\n$ composer require codinglabsau/laravel-roles\n```\n\n### Publish migrations and migrate\n```bash\nphp artisan vendor:publish --tag=\"roles-migrations\"\nphp artisan migrate\n```\n\n## Configuration\nIf you need to override the default `Role` model, you can do that by publishing the config and setting the `models.role` option.\n```\nphp artisan vendor:publish --tag=\"roles-config\"\n```\n\n## Usage\n### Add the trait\nAdd the `HasRoles` trait to your user model:\n\n```php\nuse Codinglabs\\Roles\\HasRoles;\nuse Illuminate\\Notifications\\Notifiable;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\n\nclass User extends Authenticatable\n{\n    use Notifiable, HasRoles;\n}\n```\n\n### Create roles\n```php\n$role = \\Codinglabs\\Roles\\Role::create(['name' =\u003e 'manager']);\n```\n\n### Get roles\n```php\n$managerRole = \\Codinglabs\\Roles\\Role::whereName('manager')-\u003efirst();\n```\n\n### Associate roles\nUnder the hood we are using Eloquent many-to-many relationships.\n```php\nuse Codinglabs\\Roles\\Role;\n\n// attach multiple roles\n$user-\u003eroles()-\u003eattach([\n    Role::whereName('employee')-\u003efirst()-\u003eid,\n    Role::whereName('manager')-\u003efirst()-\u003eid,\n]);\n\n// detach a single role\n$user-\u003eroles()-\u003edetach(Role::whereName('employee')-\u003efirst());\n\n// update roles to match array\n$user-\u003eroles()-\u003esync([\n    Role::whereName('employee')-\u003efirst()-\u003eid,\n]);\n\n// ensure roles in array are attached without detaching others\n$user-\u003eroles()-\u003esyncWithoutDetaching([\n    Role::whereName('employee')-\u003efirst()-\u003eid,\n]);\n```\n\n### Protect routes with middleware\nIn `App\\Http\\Kernel`, register the middeware: \n```php\nprotected $routeMiddleware = [\n    // ...\n    'role' =\u003e \\Codinglabs\\Roles\\CheckRole::class,\n];\n```\n\nAnd then call the middleware in your routes, seperating multiple roles with a pipe:\n```php\nRoute::middleware('role:employee')-\u003e...\nRoute::middleware('role:manager|admin')-\u003e...\n```\n\nOr with a gate:\n```php\nclass UserController extends Controller\n{\n    public function destroy()\n    {\n        $this-\u003eauthorize('role', 'admin');\n    }\n}\n```\n\nOr in the constructor of a controller:\n```php\nclass ManagerDashboardController extends Controller\n{\n    public function __construct()\n    {\n        $this-\u003emiddleware('role:manager');\n    }\n}\n```\n\nIf the middleware check fails, a 403 response will be returned.\n\n### Check roles on a user\nCall hasRole on the user model:\n```php\n// check a single role\n$user-\u003ehasRole('foo');\n\n// check whether any role exists\n$user-\u003ehasRole(['bar', 'baz']);\n\n// get all roles\n$user-\u003eroles;\n```\n\n### Conditionally showing content with the blade directive\n```html\n@role('admin')\n\u003cdiv\u003eSuper secret admin stuff goes here...\u003c/div\u003e\n@endrole\n```\n\n### Sharing roles with UI (Inertiajs example)\n```php\n// AppServiceProvider.php\nInertia::share([\n    'auth' =\u003e function () {\n        return [\n            'user' =\u003e Auth::user() ? [\n                'id' =\u003e Auth::user()-\u003eid,\n                'roles' =\u003e Auth::user()-\u003eroles-\u003epluck('name'),\n            ] : null\n        ];\n    }\n]);\n```\n```javascript\n// app.js\nVue.mixin({\n  methods: {\n    hasRole: function(role) {\n      return this.$page.auth.user.roles.includes(role)\n    }\n  }\n})\n```\n```html\n\u003c!-- SomeComponent.vue --\u003e\n\u003cdiv v-if=\"hasRole('manager')\"\u003eI am a manager\u003c/div\u003e\n```\n\n## Upgrading from v1 to v2\nPlease see [upgrading from v1 to v2](UPGRADING.md) for details and instructions to avoid any issues after upgrading to v2.\n\n## Contributing\nPlease see [contributing.md](contributing.md) for details and a todolist.\n\n## Security\nIf you discover any security related issues, create an issue on GitHub.\n\n## Credits\n- [Steve Thomas](https://github.com/stevethomas)\n- [All Contributors](../../contributors)\n\n## License\nMIT. Please see the [license file](LICENSE.md) for more information.\n\n## About Coding Labs\nCoding Labs is a web app development agency based on the Gold Coast, Australia. See our open source projects [on our website](https://codinglabs.com.au/open-source).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodinglabsau%2Flaravel-roles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodinglabsau%2Flaravel-roles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodinglabsau%2Flaravel-roles/lists"}