PHPackages                             youngmayor/laravel-otp - 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. youngmayor/laravel-otp

AbandonedArchivedLibrary

youngmayor/laravel-otp
======================

A laravel package for generating and validating One Time Pins

1.0.0(4y ago)040MITPHPPHP ^7.2|^8.0

Since Nov 29Pushed 4y ago1 watchersCompare

[ Source](https://github.com/YoungMayor/laravel-otp)[ Packagist](https://packagist.org/packages/youngmayor/laravel-otp)[ Docs](https://github.com/youngmayor/laravel-otp)[ RSS](/packages/youngmayor-laravel-otp/feed)WikiDiscussions main Synced 1mo ago

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

Laravel OTP
===========

[](#laravel-otp)

[![Latest Version on Packagist](https://camo.githubusercontent.com/34453dd70dcfcd4c4a889ec6cbb04892400f8616ace622d748f1475f06a8b8c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796f756e676d61796f722f6c61726176656c2d6f74702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/youngmayor/laravel-otp)[![Total Downloads](https://camo.githubusercontent.com/79bb4ee591c799cbfd2cb3b20aff9e6396d93c22283adfdd4fa7341d8d35ee4b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796f756e676d61796f722f6c61726176656c2d6f74702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/youngmayor/laravel-otp)[![GitHub Actions](https://github.com/youngmayor/laravel-otp/actions/workflows/main.yml/badge.svg)](https://github.com/youngmayor/laravel-otp/actions/workflows/main.yml/badge.svg)

A laravel package to create, validate and manage OTPs.

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

[](#installation)

You can install the package via composer:

```
composer require youngmayor/laravel-otp
```

The package uses auto discovery hence if you use Laravel 5.5 or above, installing the package automatically registers it in your application.

However, if you use Laravel 5.4 or below you will need to add the below snipet to your `config/app.php` to register the Service Provider and alias

```
'providers' => [
    // ...
    YoungMayor\LaravelOtp\LaravelOtpServiceProvider,
    // ...
],
```

Next step is to publish the configuration for the package. This can be done using the command below

```
php artisan vendor:publish --provider="YoungMayor\LaravelOtp\LaravelOtpServiceProvider"
```

This copies the package configuration into `config/laravel-otp.php`

Then you run migration to create the OTP tables

```
php artisan migrate
```

Usage
-----

[](#usage)

Models which intend to manage OTP codes should extend the `YoungMayor\LaravelOtp\Traits\OTPActions` trait

```
// ...
use YoungMayor\LaravelOtp\Traits\OTPActions;
// ...

class ExampleModel
{
    // ...
    use OTPActions;
    // ....
}
```

> It is required that the model posses an email field. This field would store the recipient of the generate OTP code. It is assumed that this field is named email. If the field has a different name, then it should be configured using the below code
>
> ```
> class ExampleModel
> {
>   use OTPActions;
>
>   public $otpEmailKey = 'user_email';
> }
> ```

OTPs can now be managed using that model.

### OTP Creation

[](#otp-creation)

```
// $exampleModel is an instance of ExampleModel
// $payload is additional data that you would like to attach to the OTP token.
// The payload can be an array, a string or an integer
$exampleModel->generatOTP('action-name', $payload);
```

This generates an OTP using the configuration defined in `config/laravel-otp.php` and emails the generate OTP to the email attached to the model

### OTP Validation

[](#otp-validation)

```
// $exampleModel is an instance of ExampleModel and has a pending OTP
// $pin is the OTP pin that is to be validated
$otp = $exampleModel->validateOTP($pin, 'action-name');
```

If the pin is correct an instance of `YoungMayor\LaravelOtp\Models\OneTimePin` would be returned, else null would be returned

```
if (!$otp) {
    // handle invalid OTP code
}
```

### Managing OTP

[](#managing-otp)

The OTP can now be managed as below

```
$otp->is_expired; // check if the OTP is expired

$payload = $otp->payload; // retrieve the OTP's Payload

$otp->refreshOTP(); // re-generate a new OTP code and send to the recipient

$otp->delete(); // delete the OTP
```

### Relationships

[](#relationships)

Every model which utilises the `OTPActions` trait have the `otps` relationship and can perform [laravel one to many](https://laravel.com/docs/8.x/eloquent-relationships#one-to-many) relationshp on it.

Likewise, from the `YoungMayor\LaravelOtp\Models\OneTimePin` instance. You can retrieve the source model using the source relationship method

```
$exampleModel = $otp->source;
```

Exceptions
----------

[](#exceptions)

The package renders two exceptions:

- `YoungMayor\LaravelOtp\Exceptions\InvalidAction` exception: Thrown when an attempt to `generateOTP()` or `validateOTP()` is made with an action that is not registered in the `config/laravel-otp.php` file.
- `YoungMayor\LaravelOtp\Exceptions\InvalidSource` exception: Thrown when the given source does not use the `OTPActions` trait

Testing
-------

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Meyoron Aghogho](https://github.com/youngmayor)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

1625d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d0ec8d6b9561eb410a931df6c434c31ccccd218db56fecb712186dc3b1ad0778?d=identicon)[YoungMayor](/maintainers/YoungMayor)

---

Top Contributors

[![YoungMayor](https://avatars.githubusercontent.com/u/47315212?v=4)](https://github.com/YoungMayor "YoungMayor (3 commits)")

---

Tags

laravel otpyoungmayor

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/youngmayor-laravel-otp/health.svg)

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

###  Alternatives

[tzsk/otp

A secure, database-free One-Time Password (OTP) generator and verifier for PHP and Laravel.

241641.4k1](/packages/tzsk-otp)[fouladgar/laravel-otp

This package provides convenient methods for sending and validating OTP notifications to users for authentication.

21426.5k](/packages/fouladgar-laravel-otp)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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