Install Hashids:

(Psst...My way of adding event listeners is a little "old-school" now, you might want to upgrade to an observable class, or adding event listeners through the model)

php artisan make:provider HashIdModelProvider

Then make sure you add this to your providers array in config/app.php:

'providers' => [
//
//
//
\App\Providers\HashIdModelProvider::class
]

Set up a column in your database.

In order to generate a HashId that's unique to the model we're creating it for, we need the model to have an id - these are assigned by the database, so we can only add a HashID after the model has been initially saved.

That means that the column that stores the HashID value will not have a value when saved initially, so it must be nullable(), for example:

The first, is a string, which will be something like \App\Post - this acts as a salt of sorts, making sure that the hash it generates is unique to this class.

If you didn't pass this, then the Post with an ID of 1, and a User with an id of 1 may end up with the same hash!

The second argument is a minimum length - by default Hashids will spit out the shortest length string possible - usually one or two characters. We want at least 10 here to make it not look silly in a URL.

(This minimum length stuff is totally optional but i prefer a longer hash by default)