PHPackages                             cybercog/laravel-optimus - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. cybercog/laravel-optimus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cybercog/laravel-optimus
========================

An Optimus bridge for Laravel. Id obfuscation based on Knuth's multiplicative hashing method.

3.12.0(1y ago)192564.1k↓10.1%14[4 issues](https://github.com/cybercog/laravel-optimus/issues)[2 PRs](https://github.com/cybercog/laravel-optimus/pulls)MITPHPPHP ^7.4|^8.0CI passing

Since Jan 2Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/cybercog/laravel-optimus)[ Packagist](https://packagist.org/packages/cybercog/laravel-optimus)[ Docs](https://komarev.com/sources/laravel-optimus)[ Fund](https://paypal.me/antonkomarev)[ RSS](/packages/cybercog-laravel-optimus/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (24)Used By (0)

Laravel Optimus
===============

[](#laravel-optimus)

[![cog-laravel-optimus](https://user-images.githubusercontent.com/1849174/28744713-5b28fffa-746f-11e7-8ca2-0e2a612bc19c.png)](https://user-images.githubusercontent.com/1849174/28744713-5b28fffa-746f-11e7-8ca2-0e2a612bc19c.png)

[![Discord](https://camo.githubusercontent.com/86e83e2f2ffe2f5caffed9d8886bb512bf42bf927dd8bb8e864d9e9983313ff7/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6f676f3d646973636f7264266c6162656c3d266d6573736167653d446973636f726426636f6c6f723d333633393366267374796c653d666c61742d737175617265)](https://discord.gg/R2sbjAt)[![Releases](https://camo.githubusercontent.com/f99d34d4eccd1bc14b192ecb9e639b9f8250de3c481dfe919f012550130462db/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6379626572636f672f6c61726176656c2d6f7074696d75732e7376673f7374796c653d666c61742d737175617265)](https://github.com/cybercog/laravel-optimus/releases)[![Build](https://camo.githubusercontent.com/18d3f3a952a27acd59b5cd0baf3d501d8ee266b83a4b68e87c03433993f9ff29/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6379626572636f672f6c61726176656c2d6f7074696d75732f74657374732e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/cybercog/laravel-optimus/actions/workflows/tests.yml)[![StyleCI](https://camo.githubusercontent.com/b1894f78f36e79dec087ae1d632e0fd5d6940cc4fbc46ccb7a0fd1af9a5c0e52/68747470733a2f2f7374796c6563692e696f2f7265706f732f37373831373835382f736869656c64)](https://styleci.io/repos/77817858)[![Code Quality](https://camo.githubusercontent.com/bf040ddd72072dc88ea8b7d33ff7413bba95e453d7046d48dff4c4ccbad27ccb/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6379626572636f672f6c61726176656c2d6f7074696d75732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/cybercog/laravel-optimus/?branch=master)[![License](https://camo.githubusercontent.com/504235d81f9b9aa99fed666654bcf72a2ce402d4a86db9e2e96b7e26274a4fc0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6379626572636f672f6c61726176656c2d6f7074696d75732e7376673f7374796c653d666c61742d737175617265)](https://github.com/cybercog/laravel-optimus/blob/master/LICENSE)

Introduction
------------

[](#introduction)

Laravel wrapper for the [Optimus Library](https://github.com/jenssegers/optimus) by [Jens Segers](https://github.com/jenssegers) with multiple connections support. Optimus is a small open-source library that generates short, unique, non-sequential ids from numbers. With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings. It is also super fast.

Contents
--------

[](#contents)

- [Features](#features)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
    - [Examples](#examples)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Testing](#testing)
- [Security](#security)
- [Contributors](#contributors)
- [Alternatives](#alternatives)
- [License](#license)
- [About CyberCog](#about-cybercog)

Features
--------

[](#features)

- Configurable multiple Optimus connections support.
- Implicit route model binding.
- Designed to work with Laravel Eloquent models.
- Dependency Injection ready.
- Includes Facade.
- Following PHP Standard Recommendations:
    - [PSR-1 (Basic Coding Standard)](http://www.php-fig.org/psr/psr-1/).
    - [PSR-2 (Coding Style Guide)](http://www.php-fig.org/psr/psr-2/).
    - [PSR-4 (Autoloading Standard)](http://www.php-fig.org/psr/psr-4/).
- Covered with unit tests.

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

[](#installation)

First, pull in the package through Composer.

```
composer require cybercog/laravel-optimus
```

#### Register Package Manually (optional)

[](#register-package-manually-optional)

If you disabled package auto-discovery you can register it manually.

Include the service provider within `app/config/app.php`.

```
'providers' => [
    Cog\Laravel\Optimus\Providers\OptimusServiceProvider::class,
],
```

If you want you can use the [facade](http://laravel.com/docs/facades). Add the reference in `config/app.php` to your aliases array.

```
'aliases' => [
    'Optimus' => Cog\Laravel\Optimus\Facades\Optimus::class,
],
```

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

[](#configuration)

Laravel Optimus requires connection configuration. To get started, you'll need to publish config file:

```
php artisan vendor:publish --provider="Cog\Laravel\Optimus\Providers\OptimusServiceProvider" --tag="config"
```

This will create a `config/optimus.php` file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.

#### Default Connection Name

[](#default-connection-name)

This option `default` is where you may specify which of the connections below you wish to use as your default connection for all work. Of course, you may use many connections at once using the manager class. The default value for this setting is `main`.

#### Optimus Connections

[](#optimus-connections)

This option `connections` is where each of the connections are setup for your application. Example configuration has been included, but you may add as many connections as you would like.

#### Optimus numbers generation

[](#optimus-numbers-generation)

To get started you will need 3 keys in main connection;

- `prime`: Large prime number lower than `2147483647`
- `inverse`: The inverse prime so that `(PRIME * INVERSE) & MAXID == 1`
- `random`: A large random integer lower than `2147483647`

Luckily for you, there is console command that can do all of this for you, just run the following command:

```
php vendor/bin/optimus spark
```

Copy-paste generated integers to your connection config.

Usage
-----

[](#usage)

#### OptimusManager

[](#optimusmanager)

This is the class of most interest. It is bound to the ioc container as `optimus` and can be accessed using the `Facades\Optimus` facade. This class implements the ManagerInterface by extending AbstractManager. The interface and abstract class are both part of [Graham Campbell's](https://github.com/GrahamCampbell) [Laravel Manager](https://github.com/GrahamCampbell/Laravel-Manager) package, so you may want to go and checkout the docs for how to use the manager class over at that repository. Note that the connection class returned will always be an instance of `Jenssegers\Optimus\Optimus`.

#### Facades\\Optimus

[](#facadesoptimus)

This facade will dynamically pass static method calls to the `optimus` object in the ioc container which by default is the `OptimusManager` class.

#### Providers\\OptimusServiceProvider

[](#providersoptimusserviceprovider)

This class contains no public methods of interest. This class should be added to the providers array in `config/app.php`. This class will setup ioc bindings.

#### Traits\\OptimusEncodedRouteKey

[](#traitsoptimusencodedroutekey)

This trait can be used in an Eloquent model to enable automatic route model binding. You can then type hint a model in a route closure or a controller and Laravel will try to find it based on the encoded ID.

### Examples

[](#examples)

Here you can see an example of just how simple this package is to use. Out of the box, the default adapter is `main`. After you enter your authentication details in the config file, it will just work:

#### Encode ID

[](#encode-id)

```
Cog\Laravel\Optimus\Facades\Optimus::encode(20); // 1535832388
```

#### Decode ID

[](#decode-id)

```
Cog\Laravel\Optimus\Facades\Optimus::decode(1535832388); // 20
```

#### Alter Optimus connection

[](#alter-optimus-connection)

The Optimus manager will behave like it is a `Jenssegers\Optimus\Optimus`. If you want to call specific connections, you can do that with the connection method:

```
use Cog\Laravel\Optimus\Facades\Optimus;

// Writing this…
Optimus::connection('main')->encode($id);

// …is identical to writing this
Optimus::encode($id);

// and is also identical to writing this.
Optimus::connection()->encode($id);

// This is because the main connection is configured to be the default.
Optimus::getDefaultConnection(); // This will return main.

// We can change the default connection.
Optimus::setDefaultConnection('alternative'); // The default is now alternative.
```

#### Dependency Injection

[](#dependency-injection)

If you prefer to use dependency injection over facades like me, then you can inject the manager:

```
use Cog\Laravel\Optimus\OptimusManager;

class Foo
{
    protected $optimus;

    public function __construct(OptimusManager $optimus)
    {
        $this->optimus = $optimus;
    }

    public function bar($id)
    {
        return $this->optimus->encode($id)
    }
}

app()->make('Foo')->bar(20);
```

#### Implicit route model binding

[](#implicit-route-model-binding)

To enable [implicit route model binding](https://laravel.com/docs/5.5/routing#implicit-binding) based on the encoded ID, all you need to do is [configure the prime numbers](#optimus-numbers-generation) and use the `Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey` trait in your model.

If you don't want to use the default Optimus connection, you can specify a custom connection by adding an `$optimusConnection` property to you model.

```
use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    use OptimusEncodedRouteKey;

    protected $optimusConnection = 'custom'; // optional
}
```

Now you can type hint your model in a route closure or controller and Laravel will use the encoded ID to query the database.

*Note: Implicit route model binding requires Laravel's `Illuminate\Routing\Middleware\SubstituteBindings` middleware, which is part of the `web` middleware group.*

```
Route::get('url/to/{model}', function (YourModel $model) {
    // ...
})->middleware('web');
```

To generate URL's to these routes you can either get the encoded route key:

```
$encodedId = $model->getRouteKey();
$url = url("url/to/{$encodedId}");
```

Or you can use named routes and pass it the model. Laravel will do the rest.

```
$url = route('my.named.route', $model);
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

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

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/phpunit
```

Security
--------

[](#security)

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

Contributors
------------

[](#contributors)

[![@antonkomarev](https://avatars.githubusercontent.com/u/1849174?s=110)
Anton Komarev](https://github.com/antonkomarev)[![@ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?s=110)
Ivan Vermeyen](https://github.com/ivanvermeyen)[![@tur-nr](https://avatars.githubusercontent.com/u/817134?s=110)
Christopher Turner](https://github.com/tur-nr)[@ahmedbally
Ahmed Bally](https://github.com/ahmedbally)[Laravel Optimus contributors list](../../contributors)

Package was inspired by [Laravel Hashids](https://github.com/vinkla/laravel-hashids) package.

This package is a wrapper for [Optimus Library](https://github.com/jenssegers/optimus).

Alternatives
------------

[](#alternatives)

- [vinkla/laravel-hashids](https://github.com/vinkla/laravel-hashids)
- [propaganistas/laravel-fakeid](https://github.com/Propaganistas/Laravel-FakeId)

*Feel free to add more alternatives as Pull Request.*

License
-------

[](#license)

- `Laravel Optimus` package is open-sourced software licensed under the [MIT license](LICENSE) by [Anton Komarev](https://komarev.com).

🌟 Stargazers over time
----------------------

[](#-stargazers-over-time)

[![Stargazers over time](https://camo.githubusercontent.com/dcc29cd6c7870952f8dc4c7f4172d3cde901e441ccaac48ef7a67fc03f2069f3/68747470733a2f2f63686172742e79687970652e6d652f6769746875622f7265706f7369746f72792d737461722f76312f37373831373835382e737667)](https://yhype.me?utm_source=github&utm_medium=cybercog-laravel-optimus&utm_content=chart-repository-star-cumulative)

About CyberCog
--------------

[](#about-cybercog)

[CyberCog](https://cybercog.su) is a Social Unity of enthusiasts. Research the best solutions in product &amp; software development is our passion.

- [Follow us on Twitter](https://twitter.com/cybercog)
- [Read our articles on Medium](https://medium.com/cybercog)

[![CyberCog](https://cloud.githubusercontent.com/assets/1849174/18418932/e9edb390-7860-11e6-8a43-aa3fad524664.png)](https://cybercog.su)

###  Health Score

59

—

FairBetter than 99% of packages

Maintenance67

Regular maintenance activity

Popularity53

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 83.1% 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 ~176 days

Recently: every ~284 days

Total

18

Last Release

419d ago

Major Versions

1.0.0 → 2.0.02017-09-09

2.1.0 → 3.0.02018-02-14

PHP version history (4 changes)1.0.0PHP ^7.0

3.0.0PHP ^7.1.3

3.6.0PHP ^7.1.3|^8.0

3.9.0PHP ^7.4|^8.0

### Community

Maintainers

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

---

Top Contributors

[![antonkomarev](https://avatars.githubusercontent.com/u/1849174?v=4)](https://github.com/antonkomarev "antonkomarev (59 commits)")[![ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?v=4)](https://github.com/ivanvermeyen "ivanvermeyen (5 commits)")[![ahmedbally](https://avatars.githubusercontent.com/u/20849424?v=4)](https://github.com/ahmedbally "ahmedbally (2 commits)")[![ronnievisser](https://avatars.githubusercontent.com/u/1931199?v=4)](https://github.com/ronnievisser "ronnievisser (2 commits)")[![binotaliu](https://avatars.githubusercontent.com/u/67255597?v=4)](https://github.com/binotaliu "binotaliu (1 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![tur-nr](https://avatars.githubusercontent.com/u/817134?v=4)](https://github.com/tur-nr "tur-nr (1 commits)")

---

Tags

cogcybercogdecryptencrypthashidsidlaravelobfuscateoptimuspackagephppublic-idurlurilaravelencryptdecrypthashidsobfuscateidcogoptimuscybercogpublic-id

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cybercog-laravel-optimus/health.svg)

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

###  Alternatives

[vinkla/hashids

A Hashids bridge for Laravel

2.1k13.3M73](/packages/vinkla-hashids)[elfsundae/laravel-hashid

A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

415246.3k2](/packages/elfsundae-laravel-hashid)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[cybercog/laravel-clickhouse

ClickHouse migrations for Laravel

163166.8k](/packages/cybercog-laravel-clickhouse)[lingxi/hashids

A Hashids bridge for Laravel

183.3k](/packages/lingxi-hashids)[bensampo/laravel-embed

Painless responsive embeds for videos, slideshows and more.

142146.8k](/packages/bensampo-laravel-embed)

PHPackages © 2026

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