PHPackages                             soulcodex/model-keyable - 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. soulcodex/model-keyable

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

soulcodex/model-keyable
=======================

Allow attach api key to laravel models

1.0.0(5y ago)076MITPHPPHP ^7.3|^7.4|^8.0

Since Feb 24Pushed 5y ago1 watchersCompare

[ Source](https://github.com/soulcodex/model-keyable)[ Packagist](https://packagist.org/packages/soulcodex/model-keyable)[ Docs](https://github.com/soulcodex/model-keyable)[ GitHub Sponsors](https://github.com/soulcodex)[ RSS](/packages/soulcodex-model-keyable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

Laravel Keyable
===============

[](#laravel-keyable)

Keyable 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.

[![Latest Stable Version](https://camo.githubusercontent.com/2c8697c94731667f0d3d05f5bc83a242c3ba96969b87fe3f7160a1c9db0cdb42/68747470733a2f2f706f7365722e707567782e6f72672f736f756c636f6465782f6d6f64656c2d6b657961626c652f76)](//packagist.org/packages/soulcodex/model-keyable) [![Total Downloads](https://camo.githubusercontent.com/925987aede9d7ae37eb45c2c17592d75e35b23ac0e7e132648f26ac6eedef8ac/68747470733a2f2f706f7365722e707567782e6f72672f736f756c636f6465782f6d6f64656c2d6b657961626c652f646f776e6c6f616473)](//packagist.org/packages/soulcodex/model-keyable) [![Latest Unstable Version](https://camo.githubusercontent.com/c0b65d23ca48f67146232fe7692ba8bc96b41429f1c4c7579612b9dae3eced61/68747470733a2f2f706f7365722e707567782e6f72672f736f756c636f6465782f6d6f64656c2d6b657961626c652f762f756e737461626c65)](//packagist.org/packages/soulcodex/model-keyable) [![License](https://camo.githubusercontent.com/54db3130c15083b553949701ec286bfdf85615962b95b054677ab89f50e2a9a7/68747470733a2f2f706f7365722e707567782e6f72672f736f756c636f6465782f6d6f64656c2d6b657961626c652f6c6963656e7365)](//packagist.org/packages/soulcodex/model-keyable)

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

[](#installation)

Require the `soulcodex/keyable` package in your `composer.json` and update your dependencies:

```
composer require soulcodex/model-keyable
```

Publish the migration and config files:

```
php artisan vendor:publish --provider="Soulcodex\Keyable\KeyableServiceProvider"
```

Run the migration:

```
php artisan migrate
```

Usage
-----

[](#usage)

Add the `Soulcodex\Keyable\Keyable` trait to your model(s):

```
use Illuminate\Database\Eloquent\Model;
use Soulcodex\Keyable\Keyable;

class Account extends Model
{
    use Keyable;

    // ...
}
```

Add the `auth.apiKey` middleware to the `mapApiRoutes()` function in your `App\Providers\RouteServiceProvider` file:

```
// ...

protected function mapApiRoutes()
{
    Route::prefix('api')
        ->middleware(['api', 'auth.apikey'])
	->namespace($this->namespace . '\API')
	->group(base_path('routes/api.php'));
}

// ...
```

The middleware will authenticate API requests, ensuring they contain an API key that is valid.

### Accessing keyable models in your controllers

[](#accessing-keyable-models-in-your-controllers)

The model associated with the key will be attached to the incoming request as `keyable`:

```
use App\Http\Controllers\Controller;

class FooController extends Controller {

    public function index(Request $request)
    {
        $model = $request->keyable;

        // ...
    }

}
```

Now you can use the keyable model to scope your associated API resources, for example:

```
return $model->foo()->get();
```

### Keys Without Models

[](#keys-without-models)

Sometimes 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:

```
