{"id":37000285,"url":"https://github.com/soulcodex/model-keyable","last_synced_at":"2026-01-14T00:02:09.124Z","repository":{"id":57056032,"uuid":"342017009","full_name":"soulcodex/model-keyable","owner":"soulcodex","description":"Add ability to attach api keys to laravel models","archived":false,"fork":false,"pushed_at":"2021-02-24T23:35:31.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T16:31:57.236Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/soulcodex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null},"funding":{"github":"soulcodex"}},"created_at":"2021-02-24T19:48:46.000Z","updated_at":"2021-02-24T23:35:33.000Z","dependencies_parsed_at":"2022-08-24T07:21:15.937Z","dependency_job_id":null,"html_url":"https://github.com/soulcodex/model-keyable","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":"spatie/package-skeleton-laravel","purl":"pkg:github/soulcodex/model-keyable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulcodex%2Fmodel-keyable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulcodex%2Fmodel-keyable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulcodex%2Fmodel-keyable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulcodex%2Fmodel-keyable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soulcodex","download_url":"https://codeload.github.com/soulcodex/model-keyable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulcodex%2Fmodel-keyable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28406468,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":[],"created_at":"2026-01-14T00:02:08.049Z","updated_at":"2026-01-14T00:02:09.110Z","avatar_url":"https://github.com/soulcodex.png","language":"PHP","funding_links":["https://github.com/sponsors/soulcodex"],"categories":[],"sub_categories":[],"readme":"# Laravel Keyable\n\nKeyable is a package that allows you to add API Keys to any model. This allows you to associate incoming requests with their respective models. You can also use Policies to authorize requests.\n\n[![Latest Stable Version](https://poser.pugx.org/soulcodex/model-keyable/v)](//packagist.org/packages/soulcodex/model-keyable) [![Total Downloads](https://poser.pugx.org/soulcodex/model-keyable/downloads)](//packagist.org/packages/soulcodex/model-keyable) [![Latest Unstable Version](https://poser.pugx.org/soulcodex/model-keyable/v/unstable)](//packagist.org/packages/soulcodex/model-keyable) [![License](https://poser.pugx.org/soulcodex/model-keyable/license)](//packagist.org/packages/soulcodex/model-keyable)\n\n## Installation\n\nRequire the ```soulcodex/keyable``` package in your ```composer.json``` and update your dependencies:\n\n```bash\ncomposer require soulcodex/model-keyable\n```\n\nPublish the migration and config files:\n```bash\nphp artisan vendor:publish --provider=\"Soulcodex\\Keyable\\KeyableServiceProvider\"\n```\n\nRun the migration:\n```bash\nphp artisan migrate\n```\n\n## Usage\n\nAdd the ```Soulcodex\\Keyable\\Keyable``` trait to your model(s):\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Soulcodex\\Keyable\\Keyable;\n\nclass Account extends Model\n{\n    use Keyable;\n\n    // ...\n}\n```\n\nAdd the ```auth.apiKey``` middleware to the ```mapApiRoutes()``` function in your ```App\\Providers\\RouteServiceProvider``` file:\n\n```php\n// ...\n\nprotected function mapApiRoutes()\n{\n    Route::prefix('api')\n        -\u003emiddleware(['api', 'auth.apikey'])\n\t-\u003enamespace($this-\u003enamespace . '\\API')\n\t-\u003egroup(base_path('routes/api.php'));\n}\n\n// ...\n```\n\nThe middleware will authenticate API requests, ensuring they contain an API key that is valid.\n\n### Accessing keyable models in your controllers\nThe model associated with the key will be attached to the incoming request as ```keyable```:\n\n```php\nuse App\\Http\\Controllers\\Controller;\n\nclass FooController extends Controller {\n\n    public function index(Request $request) \n    {\n        $model = $request-\u003ekeyable;\n\n        // ...\n    }\n\n}\n```\nNow you can use the keyable model to scope your associated API resources, for example:\n```php\nreturn $model-\u003efoo()-\u003eget();\n```\n\n### Keys Without Models\n\nSometimes you may not want to attach a model to an API key (if you wanted to have administrative access to your API). By default this functionality is turned off:\n\n```php\n\u003c?php\n\t\nreturn [\n\t\n    'allow_empty_models' =\u003e true\n\t\n];\n```\n\n### UUID support\nBefore migrate you can config if you prefer use bigint or uuid identifiers.\nBy default use `bigint` like ***keyable_id***\n\n```php\n\u003c?php\n\nreturn [\n\n    'identifier' =\u003e 'bigint'\n    \n];\n```\n\n## Making Requests\n\nBy default, laravel-keyable uses bearer tokens to authenticate requests. Attach the API key to the header of each request:\n\n```\nAuthorization: Bearer \u003ckey\u003e\n```\n\nYou can change where the API key is retrieved from by altering the setting in the `keyable.php` config file. Supported options are: `bearer`, `header`, and `parameter`.\n\nAs it is an array, you can use more than one of these options and combine them.\n\n```php\n\u003c?php\n\t\nreturn [\n\t\n    'modes' =\u003e ['header'],\n\t\n    'key' =\u003e 'X-Authorization',\n\t\n];\n```\n\nNeed to pass the key as a URL parameter? Set the mode to `parameter` and the key to the string you'll use in your URL:\n```php\n\u003c?php\n\t\nreturn [\n\t\n    'modes' =\u003e ['parameter'],\n\t\n    'key' =\u003e 'api_key'\n\t\n];\n```\nNow you can make requests like this:\n```php\nhttps://example.com/api/posts?api_key=\u003ckey\u003e\n```\n\n## Authorizing Requests\n\nLaravel offers a great way to perform [Authorization](https://laravel.com/docs/5.8/authorization) on incoming requests using Policies. However, they are limited to authenticated users. We replicate that functionality to let you authorize requests on any incoming model.\n\nTo begin, add the `AuthorizeKeyableRequest` trait to your base `Controller.php class`:\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\n// ...\n\nuse Soulcodex\\Keyable\\Auth\\AuthorizeKeyableRequest;\n\nclass Controller extends BaseController\n{\n    use AuthorizeKeyableRequest;\n}\n```\n\nNext, create the `app/Policies/KeyablePolicies` folder and create a new policy:\n\n```php\n\u003c?php\n\nnamespace App\\Policies\\KeyablePolicies;\n\nuse App\\Models\\Post;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Soulcodex\\Keyable\\Models\\ApiKey;\n\nclass PostPolicy {\n\n    public function view(ApiKey $apiKey, Model $keyable, Post $post) {\n    \treturn !is_null($keyable-\u003eposts()-\u003efind($post-\u003eid));\n    }\n    \n}\n```\n\nLastly, register your policies in `AuthServiceProvider.php`:\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\n// ...\n\nuse App\\Models\\Post;\nuse App\\Policies\\KeyablePolicies\\PostPolicy;\nuse Soulcodex\\Keyable\\Facades\\Keyable;\n\nclass AuthServiceProvider extends ServiceProvider\n{\n\t\n    // ...\n    \n    protected $keyablePolicies = [\n        Post::class =\u003e PostPolicy::class\n    ];\n\n    public function boot(GateContract $gate)\n    {\n        // ...\n        Keyable::registerKeyablePolicies($this-\u003ekeyablePolicies);\n    }\n    \n}\n```\n\nIn your controller, you can now authorize the request using the policy by calling `$this-\u003eauthorizeKeyable(\u003cability\u003e, \u003cmodel\u003e)`:\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers\\PostController;\n\nuse App\\Models\\Post;\nuse Illuminate\\Http\\Request;\nuse App\\Http\\Controllers\\Controller;\n\nclass PostController extends Controller {\n\n    public function show(Post $post) {\n        $this-\u003eauthorizeKeyable('view', $post);\n        // ...\n    }\n\n}\n```\n\n## Artisan Commands\n\nGenerate an API key:\n\n```bash\nphp artisan api-key:generate --id=1 --type=\"App\\Models\\Account\"\nphp artisan api-key:generate --id='6324d582-5614-430b-a35c-c24b621a93c5' --type=\"App\\Models\\Account\"\n```\n\nDelete an API key:\n```bash\nphp artisan api-key:delete --id=12345\nphp artisan api-key:delete --id='6324d582-5614-430b-a35c-c24b621a93c5'\n```\n\n## Security\n\nIf you discover any security related issues, please email [info@soulcodex.es](mailto:info@soulcodex.es).\n\n## License\nReleased under the [MIT](https://choosealicense.com/licenses/mit/) license. See [LICENSE](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoulcodex%2Fmodel-keyable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoulcodex%2Fmodel-keyable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoulcodex%2Fmodel-keyable/lists"}