PHPackages                             pacificinternet/laravel-hashslug - 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. pacificinternet/laravel-hashslug

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

pacificinternet/laravel-hashslug
================================

Package providing a trait to use Hashids on a model

1.0.0(2y ago)013GPL-3.0-onlyPHP

Since Jul 17Pushed 2y agoCompare

[ Source](https://github.com/pacificdevelopers/laravel-hashslug)[ Packagist](https://packagist.org/packages/pacificinternet/laravel-hashslug)[ RSS](/packages/pacificinternet-laravel-hashslug/feed)WikiDiscussions master Synced 1mo ago

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

Laravel HashSlug (Hashids)
==========================

[](#laravel-hashslug-hashids)

Forked from  v2.2.4

This package is useful to hide real model ids in urls using [Hashids](https://github.com/ivanakimov/hashids.php). A hashid (slug) is deterministically generated given an application, a model class and an id. Also, given a hashid (slug), the real id can be decoded. Thus no extra field needs to be stored in the database, ids are decoded on each request.

Generates urls on the fly

```
database -> id (1) -> hashslug (K4Nkd) -> url (http://localhost/posts/K4Nkd)

```

Decodes hashids and finds models on the fly

```
url (http://localhost/posts/K4Nkd) -> hashslug (K4Nkd) -> id (1) -> database -> model

```

Hashslugs have the following properties:

1. It is guaranteed that hashslugs are unique per id
2. It is guaranteed that for different models, different series of hashslugs are generated (a post of id 1 will have a different hashslug as a comment with id 1)
3. It is guaranteed that for different installations, different series of hashslugs are generated (depending on app key in the `.env` file)

It is important to note that hashids are **not random, nor unpredictable**. Do not use this package if that's a concern. Quoting from [hashids.org](http://hashids.org/#decoding):

> Do you have a question or comment that involves "security" and "hashids" in the same sentence? Don't use Hashids.

However, although hashslug encoding depends on the app key, it cannot be exposed by an attacker, since it's [sha256 hashed](https://github.com/balping/laravel-hashslug/blob/bc66a9c5f635ef7d6d4b5e904529c3c26194ce99/src/HasHashSlug.php#L70) before passing it to Hashids. Your app key is safe.

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

[](#installation)

```
composer require balping/laravel-hashslug
```

### Versions

[](#versions)

LaravelHashslug5.4.\*2.0.\*5.5 - 5.82.1.\*6.\*2.1.\*7.\*2.2.\*8.\*2.2.\*9.\*2.2.\*10.\*2.2.\*> **Note:** This package requires either the [BC Math](https://secure.php.net/manual/en/book.bc.php) or [GMP](https://secure.php.net/manual/en/book.gmp.php) extension in order to work.

Usage
-----

[](#usage)

Include trait on a model that you wish to have hashid slugs to hide numeric incremental ids.

```
use Illuminate\Database\Eloquent\Model;
use Balping\HashSlug\HasHashSlug;

class Post extends Model {
    use HasHashSlug;
}
```

After this, functions `slug()`, `findBySlug($slug)` and `findBySlugOrFail($slug)` are added to your model.

Every time you generate a url using Laravel's helpers, instead of numeric ids, hashids are used (with the default length of 5 characters):

```
// routes/web.php
Route::resource('/posts', 'PostController');

// somewhere else
$post = Post::first();
echo action('PostController@show', $post);
// prints http://localhost/posts/K4Nkd
```

Then you can resolve the model by the slug.

```
// app/Http/Controllers/PostController.php

public function show($slug){
    $post = Post:findBySlugOrFail($slug);

    return view('post.show', compact('post'));
}
```

You can use [implicit model binding](https://laravel.com/docs/master/routing#implicit-binding) too. You don't have to do anything, it works automatically!

Just typehint models and they are automatically resolved:

```
// routes/web.php
Route::resource('/posts', 'PostController');

// app/Http/Controllers/PostController.php
public function show(Post $post){
    return view('post.show', compact('post'));
}
```

If you need [explicit model binding](https://laravel.com/docs/master/routing#explicit-binding), that's also convenient:

```
//app/Providers/RouteServiceProvider.php
public function boot(){
    parent::boot();

    Route::model('article', App\Post::class);
}

// routes/web.php
Route::resource('/articles', 'PostController');

// app/Http/Controllers/PostController.php
public function show(Post $post){
    return view('post.show', compact('post'));
}
```

Customisation
-------------

[](#customisation)

### Salts

[](#salts)

The uniqueness of hashslug series per model and app installation depends on having unique salts.

By default, the salt passed to Hashids depends on the app key defined in `.env` and the class name of the model.

#### Application salt

[](#application-salt)

To change the 'application salt', create file `config/hashslug.php` then add the following code:

```
