PHPackages                             davide7h/encoded-ids - 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. [Security](/categories/security)
4. /
5. davide7h/encoded-ids

ActiveLibrary[Security](/categories/security)

davide7h/encoded-ids
====================

An easy way to mask your IDs when exposing them in routes, APIs or other scenarios when you don't want the user to see the actual ID of your database records

1.0.6(2y ago)06MITPHPPHP ^8.1

Since Apr 4Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Davide7h/encoded-ids)[ Packagist](https://packagist.org/packages/davide7h/encoded-ids)[ RSS](/packages/davide7h-encoded-ids/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (6)Used By (0)

[![Logo](https://camo.githubusercontent.com/0f8ae4ae4df254da15642757b7d5e464290edb077ffc14968e29185dd07998e5/68747470733a2f2f692e6962622e636f2f6e6a5a714e62352f656e636f6465642d6964732e77656270)](https://camo.githubusercontent.com/0f8ae4ae4df254da15642757b7d5e464290edb077ffc14968e29185dd07998e5/68747470733a2f2f692e6962622e636f2f6e6a5a714e62352f656e636f6465642d6964732e77656270)

ID Masking for your Eloquent Models
===================================

[](#id-masking-for-your-eloquent-models)

[![MIT License](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://choosealicense.com/licenses/mit/)[![Stability](https://camo.githubusercontent.com/7bbe1e23322cbe92accb2337958b1ff0fbd443fbb6de1bee904961e37ca66e75/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4672616d65776f726b2d4c61726176656c2d7265642e737667)](https://getcomposer.org/doc/04-schema.md#minimum-stability)[![Latest Version](https://camo.githubusercontent.com/419eb95fbe56604bd776ff816ac1c7334c098d61bbfcbfcbbae76aa511b99bb4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61746573745f56657273696f6e2d312e302e362d6666656430302e737667)](https://getcomposer.org/doc/04-schema.md#minimum-stability)[![Stability](https://camo.githubusercontent.com/34901ad264261c036edcb34ecf9826470a67a173d6def4d0bc69b7329f9b1033/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d696e5f53746162696c6974792d616c7068612d626c75652e737667)](https://getcomposer.org/doc/04-schema.md#minimum-stability)

Documentation, Installation, and Usage Instructions
---------------------------------------------------

[](#documentation-installation-and-usage-instructions)

### What It Does

[](#what-it-does)

This package allows you to use a convenient Trait to instantly enable all your Eloquent Models with encoded ids for you to use in routes and api endpoints without exposing to your users the real IDs for your database records.

### Requirements

[](#requirements)

This package is under active development, therefore its requirements may be subject to change. At the moment it's been successfully tested on:

- Laravel ^8.x | ^9.x | ^10.x
- PHP &gt;= 8.1

### Important Security Notes

[](#important-security-notes)

This package requires [Sqids](https://sqids.org/) package for hashing the numeric IDs. It will be automatically installed.

It's important to reiterate what the authors of the Sqids package clearly state on their website: **this is not an encryption library**, and the generated IDs can **still** be deciphered if an attacker were to find out what alphabet was used to generate it. It only serves a cosmetic purpose, rendering IDs less obvious and more hard to guess, but not cryptographically encrypted.

**Do not use it to hide sensitive information!**

### Installation

[](#installation)

Just run `composer require davide7h/encoded-ids`

This will download and extract the package and its dependencies ([Sqids](https://sqids.org/)). The auto-discovery feature will automatically register the package ServiceProvider.

#### Generating Alphabet and publishing configurations

[](#generating-alphabet-and-publishing-configurations)

`php artisan encoded-ids:install`.

This command will generate a randomly shuffled alphabet that will be used to encode and decode your IDs.

It will also publish the config file `config/encoded-ids.php`. Here you will find:

- the newly generated **alphabet** (in case you need to alter it or replace it with an entirely new one)
- the **padding** key, used to specify a *minimum length* for the encoded id (default: 8char)

That's it, you're good to go!

Usage
-----

[](#usage)

### Encoding Model IDs

[](#encoding-model-ids)

Providing your Models with the ability to display a masked version of their actual IDs is as simple as using the `HasEncodedId` Trait:

```
