PHPackages                             hith/laravel-codeable - 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. [Database &amp; ORM](/categories/database)
4. /
5. hith/laravel-codeable

ActiveLibrary[Database &amp; ORM](/categories/database)

hith/laravel-codeable
=====================

A lightweight Eloquent trait for generating, storing, expiring, and managing numeric codes (OTPs, tokens) on any model.

v1.1.1(5mo ago)00MITPHPPHP ^8.2CI passing

Since Nov 27Pushed 5mo agoCompare

[ Source](https://github.com/hith-hj/laravel-codeable)[ Packagist](https://packagist.org/packages/hith/laravel-codeable)[ RSS](/packages/hith-laravel-codeable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

---

Laravel Codeable
----------------

[](#laravel-codeable)

Laravel Codeable focuses on **Code generation for models** such as OTPs.

### Supported Laravel versions

[](#supported-laravel-versions)

Laravel VersionCodeable Version12.x1.0+Getting Started
---------------

[](#getting-started)

> **Requires:**

- **[PHP 8.2+](https://php.net/releases/)**
- **[Laravel 12.x+](https://github.com/laravel/laravel)**

**1**: Use [Composer](https://getcomposer.org) to install laravel-codeable into your project:

```
composer require "hith/laravel-codeable"
```

**2**: Then, publish files:

```
php artisan vendor:publish --tag=codeable-migrations
php artisan vendor:publish --tag=codeable-command
php artisan vendor:publish --tag=codeable-config
```

For simplicity, you can publish all files at once:

```
php artisan vendor:publish --tag=codeable-files
```

**3**: Finally, use the package:

- By adding the trait directly to your models.
- By using the Codeable class.
- By using the Coder Facade.

### Usage of HasCodes Trait:

[](#usage-of-hascodes-trait)

In Model:

```
use Codeable\Traits\HasCodes;

class User extends Model
{
    use HasCodes;
}
```

Then:

```
$user->createCode();
$code = $user->createCode(type:'code_type', length:6, timeToExpire:'15:m'); // or define the attributes you want
$user->deleteCode($code); // delete by passing a Code model instance
$user->deleteCode('code_type'); // delete by code type
$user->deleteCode($code->id); // delete by code id
```

### Usage of Codeable Class:

[](#usage-of-codeable-class)

```
use Codeable\Codeable;

$codeable = new Codeable();
$codeable->createCode();
$code = $codeable->createCode(type:'code_type', length:6, timeToExpire:'15:m');
$codeable->deleteCode($code);
$codeable->deleteCode('code_type');
```

### Usage of Coder Facade:

[](#usage-of-coder-facade)

```
use Codeable\Facades\Coder;

$code = Coder::createCode();
if(! $code->isValid()){
    Coder::delete($code);
}

$code = Coder::codeByType('code_type');
if($code->isExpired()){
    Coder::delete($code);
}
```

### Useful Methods

[](#useful-methods)

These methods let you easily retrieve codes. They can be used with the Model trait, the Codeable class, or the Coder Facade.

```
// Get a code by type or by code value
$user->code('code_type');
$user->code(12345);

// Or use explicit methods
$user->codeByType('code_type');
$user->codeByCode(12345);

// Or fetch it by ID
$user->codeById($code->id);
```

Codeable: Delete Expired Command
--------------------------------

[](#codeable-delete-expired-command)

This command removes all expired codes from the database.

### Usage

[](#usage)

Run the following in your terminal:

```
php artisan codes:delete-expired
```

Codeable Configuration
----------------------

[](#codeable-configuration)

You can alter the behavior using the Codeable config file.

```
/**
 *  min_length   : int
 *  The minimum number of digits allowed in a generated code.
 * */
'min_length' => 3,

/**
 *  max_length   : int
 *  The maximum number of digits allowed in a generated code.
 * */
'max_length' => 16,

/**
 *  max_attempts : int
 *  The maximum number of attempts to generate a unique code before failing.
 *  Example: 5 → after 5 unsuccessful tries, any code will be returned.
 * */
'max_attempts' => 1,

/**
 *   valid_units  : string[]
 *   Allowed time units for setting the `expire_at` field in the database record.
 */
'valid_units' => [
    's' => 'second',
    'm' => 'minute',
    'h' => 'hour',
    'd' => 'day',
]
```

Contributing
------------

[](#contributing)

Thank you for considering contributing to Laravel Codeable. All the contribution guidelines are mentioned [here](CONTRIBUTING.md).

License
-------

[](#license)

Laravel Codeable is an open-sourced software licensed under the [MIT license](LICENSE.md).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance70

Regular maintenance activity

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 71.4% 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 ~1 days

Total

2

Last Release

171d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/824961a563ed0c70aa30c15d1e1823448d21fc7bc7f1832451b7caf413a894cc?d=identicon)[hith-hj](/maintainers/hith-hj)

---

Top Contributors

[![hith-hj](https://avatars.githubusercontent.com/u/52906481?v=4)](https://github.com/hith-hj "hith-hj (5 commits)")[![semantic-release-bot](https://avatars.githubusercontent.com/u/32174276?v=4)](https://github.com/semantic-release-bot "semantic-release-bot (2 commits)")

---

Tags

laraveltokensotpeloquentcodes

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/hith-laravel-codeable/health.svg)

```
[![Health](https://phpackages.com/badges/hith-laravel-codeable/health.svg)](https://phpackages.com/packages/hith-laravel-codeable)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[io238/laravel-iso-countries

Ready-to-use Laravel models and relations for country (ISO 3166), language (ISO 639-1), and currency (ISO 4217) information with multi-language support.

5462.3k](/packages/io238-laravel-iso-countries)

PHPackages © 2026

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