PHPackages                             givebutter/laravel-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. [HTTP &amp; Networking](/categories/http)
4. /
5. givebutter/laravel-keyable

ActiveLibrary[HTTP &amp; Networking](/categories/http)

givebutter/laravel-keyable
==========================

Add API keys to your Laravel models

v3.1.1(12mo ago)189158.0k↓33.5%24[3 PRs](https://github.com/givebutter/laravel-keyable/pulls)1MITPHPPHP ^7.0|^8.0CI failing

Since May 3Pushed 12mo ago21 watchersCompare

[ Source](https://github.com/givebutter/laravel-keyable)[ Packagist](https://packagist.org/packages/givebutter/laravel-keyable)[ Docs](https://github.com/givebutter/laravel-keyable)[ RSS](/packages/givebutter-laravel-keyable/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (2)Versions (17)Used By (1)

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

[](#laravel-keyable)

Laravel 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/51306c597c168a23553371d7d0a8f03f9aed49ef4b407de377bb285cbe0f2159/68747470733a2f2f706f7365722e707567782e6f72672f676976656275747465722f6c61726176656c2d6b657961626c652f762f737461626c65)](https://packagist.org/packages/givebutter/laravel-keyable) [![Total Downloads](https://camo.githubusercontent.com/32f89d2769ee8c2c1aa0a5bd3b7e0d3ba7afeeb8765561d9098edb3434ce2408/68747470733a2f2f706f7365722e707567782e6f72672f676976656275747465722f6c61726176656c2d6b657961626c652f646f776e6c6f616473)](https://packagist.org/packages/givebutter/laravel-keyable) [![License](https://camo.githubusercontent.com/c7d7ca12f36a951cc7646e584f582c297a19cb6c22bc4f8acc5ac93a67dd5406/68747470733a2f2f706f7365722e707567782e6f72672f676976656275747465722f6c61726176656c2d6b657961626c652f6c6963656e7365)](https://packagist.org/packages/givebutter/laravel-keyable)

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

[](#installation)

Require the `givebutter/laravel-keyable` package in your `composer.json` and update your dependencies:

```
composer require givebutter/laravel-keyable
```

Publish the migration and config files:

```
php artisan vendor:publish --provider="Givebutter\LaravelKeyable\KeyableServiceProvider"
```

Run the migration:

```
php artisan migrate
```

Usage
-----

[](#usage)

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

```
use Illuminate\Database\Eloquent\Model;
use Givebutter\LaravelKeyable\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.

### Generating API keys

[](#generating-api-keys)

You can generate new API keys by calling the `createApiKey()` method from the `Keyable` trait.

When you do so, it returns an instance of `NewApiKey`, which is a simple class the contains the actual `ApiKey` instance that was just created, and also contains the plain text api key, which is the one you should use to authenticate requests.

```
$newApiKey = $keyable->createApiKey();

$newApiKey->plainTextApiKey // This is the key you should use to authenticate requests
$newApiKey->apiKey // The instance of ApiKey just created
```

You can also manually create API keys without using the `createApiKey` from the `Keyable` trait, in that case, the instance you get back will have a property called `plainTextApikey` populated with the plain text API key.

```
$myApiKey = ApiKey::create([
    'keyable_id' => $account->getKey(),
    'keyable_type' => Account::class,
    'name' => 'My api key',
]);

$myApiKey->plainTextApikey // Token to be used to authenticate requests
```

Keep in mind `plainTextApikey` will only be populated immediately after creating the key.

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

```
