PHPackages                             stokoe/tokenable - 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. [API Development](/categories/api)
4. /
5. stokoe/tokenable

AbandonedArchivedLibrary[API Development](/categories/api)

stokoe/tokenable
================

Add API tokens to any model

1.1(6y ago)4101MITPHP

Since Jul 12Pushed 6y ago1 watchersCompare

[ Source](https://github.com/Stokoe0990/laravel-tokenable)[ Packagist](https://packagist.org/packages/stokoe/tokenable)[ RSS](/packages/stokoe-tokenable/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

Laravel-Tokenable
=================

[](#laravel-tokenable)

Easy creation of API Tokens for your Laravel models.

This package is aimed at those who want to build their own authentication systems. Or just need API Tokens for something small and don't want to pull in the entirity of `laravel/passport` for example.

It works by using a trait on whichever model(s) you wish to generate tokens for. The trait contains a one-to-many polymorphic relationship to the API Token model. It also contains a couple of helper functions which should be useful in utilising this package.

---

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

[](#installation)

`$ composer require stokoe/tokenable`

Publish assets
--------------

[](#publish-assets)

`$ php artisan vendor:publish --provider="Stokoe\TokenableServiceProvider"`

---

Configuration
-------------

[](#configuration)

### API Token length

[](#api-token-length)

Open up `config/tokenable.php`:

```
return [
	'token_length' => 60,
...
```

### Set New Tokens As Primary On Creation

[](#set-new-tokens-as-primary-on-creation)

Open up `config/tokenable.php`:

```
return [
...
	'make_primary' => true,
...
```

---

Usage
-----

[](#usage)

Import the trait into your model and use it:

```
namespace App;

...
use Stokoe\Tokenable;

class User extends Authenticatable
{
	use Notifiable, Tokenable;
...
```

Now that model has a relationship called `apiKey`.

### Generate new API Token for a given record.

[](#generate-new-api-token-for-a-given-record)

Generate an API token for given record.

```
$user = App\User::first();
$user->generateApiToken();
// return ex: d96d7fb36e394c22bab8d4089f619752a3fe172effbdaad738d4276d81df72305373e207a7a91f8e18fc32cf1f9b6c6977d540f9a125c0746101d539

// You can also set token length at time of generation by passing an integer to the generate method.
$user->generateApiToken(12);
// return ex: 4bfc264da5337994341981a2
```

Get the API Tokens related to the record
----------------------------------------

[](#get-the-api-tokens-related-to-the-record)

Normally you'd find an API key by looking up users like:

```
$user = User::where('api_token', $token)->first();
```

or similar. In this case it's not so simple. There is a method provided but you may write your own inverse relationship. (Or improve mine and make a pull request!)

Here's how to find the model related to an API token.

```
$user = App\User::first();
$user->api_tokens; // Returns all API Tokens related to the user. (in this example)

$token = $request->input('token');
$relatedModel = $token->getRelatedModel(); // I don't like how this method works...

// return ex: d96d7fb36e394c22bab8d4089f619752a3fe172effbdaad738d4276d81df72305373e207a7a91f8e18fc32cf1f9b6c6977d540f9a125c0746101d539
```

Set an API Token as the primary token for that record
-----------------------------------------------------

[](#set-an-api-token-as-the-primary-token-for-that-record)

```
$user = User::first();
$user->api_tokens->sort('created_at', 'desc')->first()->setPrimary(true);

$user->generateApiToken(null, true); // Create an API token for that user, use the standard length (defined in config) and make it primary.
```

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

2546d ago

Major Versions

0.1 → 1.12019-07-12

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/38764871?v=4)[stokoe](/maintainers/stokoe)[@StokoE](https://github.com/StokoE)

---

Top Contributors

[![Michael-Stokoe](https://avatars.githubusercontent.com/u/2981213?v=4)](https://github.com/Michael-Stokoe "Michael-Stokoe (8 commits)")

---

Tags

apilaravelphptokens

### Embed Badge

![Health badge](/badges/stokoe-tokenable/health.svg)

```
[![Health](https://phpackages.com/badges/stokoe-tokenable/health.svg)](https://phpackages.com/packages/stokoe-tokenable)
```

###  Alternatives

[exsyst/swagger

A php library to manipulate Swagger specifications

35916.4M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24016.2M19](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172445.0k11](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

93459.5k6](/packages/botman-driver-telegram)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
