PHPackages                             ashkanrimer/hashids - 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. ashkanrimer/hashids

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

ashkanrimer/hashids
===================

Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers

4.3.0(4y ago)04411MITPHPPHP ^8.0

Since Jul 22Pushed 4y agoCompare

[ Source](https://github.com/ashkanRimer/hashids)[ Packagist](https://packagist.org/packages/ashkanrimer/hashids)[ Docs](https://hashids.org/php)[ RSS](/packages/ashkanrimer-hashids/feed)WikiDiscussions master Synced 1mo ago

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

[![hashids](https://camo.githubusercontent.com/c18b3677d02769b80412ab8398f7363896ce557caeac8453ee8249cd68b2c6d1/68747470733a2f2f686173686964732e6f72672f7075626c69632f696d672f686173686964732e676966 "Hashids")](https://hashids.org/)

[![Build Status](https://camo.githubusercontent.com/6efdf203ba0f8378cc2453df525837f714c96f71e9e6588713296abfadc5ead7/68747470733a2f2f62616467656e2e6e65742f6769746875622f636865636b732f76696e6b6c612f686173686964733f6c6162656c3d6275696c642669636f6e3d676974687562)](https://github.com/vinkla/hashids/actions)[![Monthly Downloads](https://camo.githubusercontent.com/fb34d1cd660b022a122b30f006da2cf4efd90c057be336cdc6d5c2e701688ac8/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f646d2f686173686964732f68617368696473)](https://packagist.org/packages/hashids/hashids/stats)[![Latest Version](https://camo.githubusercontent.com/8e6f5aa3d339dd83f6ef6c4a7434abd804a998f283995f26ef6524010433d5cd/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f686173686964732f68617368696473)](https://packagist.org/packages/hashids/hashids)

**Hashids** is a small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database numeric ids to users:

This is a wrapper around the original `hashids/hashids` package. For basic documentation refer to the original one.

The above badges are for the original package.

Installation
============

[](#installation)

Require this package, with [Composer](https://getcomposer.org), in the root directory of your project.

```
$ composer require dutymess/hashids
```

Then you can enjoy the full features of the [original package](https://github.com/vinkla/hashids). Refer to their documentation for more details.

Herein, I just describe what has been added to it.

Advantages Over the Original Package
====================================

[](#advantages-over-the-original-package)

1. It's configured for the most-appropriate usages, with alphabets and lengths already set out of the box.
2. The encoder doesn't return an array for a single id. You don't have to worry about array/string conversions.
3. Bypasser is a great tool if you face trouble.
4. The model trait can be used on eloquent models and easily provide hashid tools.
5. You can still enjoy all the benefits provided by the original package, without any overheads.

Basic Usage
===========

[](#basic-usage)

The first thing you need to do is to define your salt in your `.env` file.

```
HASHID_SALT_MAIN=SomeSaltSomeSaltSomeSaltSomeSaltSomeSaltSomeSaltSomeSalt

```

Hashids must not be used as a means of security, but it is a good practice to redefine the salt per app.

Then, you are ready to convert ids and hashids to each other, through easy helper functions.

```
hashid(1); 		// returns the hadhis of 1. ex: rolXo
hashid('rolXo') // returns the equivalent id: 1
```

No matter what the input is, the `hashid` function will always switch.

Use the below methods, if you want to be sure the returned data is of the desired type.

```
hashid_number(1);        // returns 1
hashid_number('rolXo');  // returns 1
hashid_string('rolXo');  // returns 'rolXo'
hashid_string(1);        // returns 'rolXo'
```

All the above methods receive arrays as well.

```
hashid([1,2,3]);     // returns ["rolXo", "bVnxk", "LkGjo"]
```

Model Helpers
=============

[](#model-helpers)

As the most important usage of this package is to hide table `id`s, we have introduced a special trait to be used in your model.

```
