A Hashids bridge for Laravel

Overview

hashids

Laravel Hashids

A Hashids bridge for Laravel.

// Encode integers.
Hashids::encode(4815162342);

// Decode strings.
Hashids::decode('1LLb3b4ck');

// Dependency injection example.
$hashidsManager->encode(911);

Build Status Monthly Downloads Latest Version

Installation

Require this package, with Composer, in the root directory of your project.

composer require vinkla/hashids

Configuration

Laravel Hashids requires connection configuration. To get started, you'll need to publish all vendor assets:

$ php artisan vendor:publish

This will create a config/hashids.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

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.

Hashids 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.

Usage

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

// You can alias this in config/app.php.
use Vinkla\Hashids\Facades\Hashids;

// We're done here - how easy was that, it just works!
Hashids::encode(4815162342);

// This example is simple and there are far more methods available.
Hashids::decode('doyouthinkthatsairyourebreathingnow');

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

use Vinkla\Hashids\Facades\Hashids;

// Writing this...
Hashids::connection('main')->encode($id);

// ...is identical to writing this
Hashids::encode($id);

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

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

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

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

use Vinkla\Hashids\HashidsManager;

class Foo
{
    protected $hashids;

    public function __construct(HashidsManager $hashids)
    {
        $this->hashids = $hashids;
    }

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

App::make('Foo')->bar();

For more information on how to use the Hashids\Hashids class, check out the docs at hashids/hashids.

Comments
  • exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'parse error, expecting `';'' or `'{''' in /vendor/vinkla/hashids/src/HashidsServiceProvider.php:121

    exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'parse error, expecting `';'' or `'{''' in /vendor/vinkla/hashids/src/HashidsServiceProvider.php:121

    I get this error on log whenever some commands are running in laravel, the application is working as expected but in log it is getting created as soon as there is command fires

    opened by msonowal 13
  • Lumen 5.2 Issue.

    Lumen 5.2 Issue.

    There's some problem when using with Lumen 5.2, here's the error it's throwing:

    [ErrorException]
      Argument 1 passed to Vinkla\Hashids\HashidsServiceProvider::registerFactory() must be an instance of Illuminate\Contracts\Foundation\Application, i
      nstance of Laravel\Lumen\Application given, called in /Users/User/Projects/Code/lumen/vendor/vinkla/hashids/src/HashidsServiceProvider.php on line 6
      4 and defined
    
    opened by irazasyed 10
  • Laravel 5.5 cannot install hashids

    Laravel 5.5 cannot install hashids

    Laravel 5.5 cannot install hashids Error message: Your requirements could not be resolved to an installable set of packages.

      Problem 1     - Installation request for vinkla/hashids ^5.0 -> satisfiable by vinkla/hashids[5.0.0].     - Conclusion: remove laravel/framework v5.5.40     - Conclusion: don't install laravel/framework v5.5.40     - vinkla/hashids 5.0.0 requires illuminate/support 5.6.* -> satisfiable by illuminate/support[v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, V5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5. 6.27, v5.6.28, v5.6.3, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9].     - don't install illuminate/support v5.6.4|don't install laravel/framework v5.5.40     - don't install illuminate/support v5.6.5|don't install laravel/framework v5.5.40     - don't install illuminate/support v5.6.6|don't install laravel/framework v5.5.40     - don't install illuminate/support v5.6.7|don't install laravel/framework v5.5.40     - don't install illuminate/support v5.6.0|don't install laravel/framework v5.5.40     - don't install illuminate/support v5.6.1|don't install laravel/framework v5.5.40     - illuminate/support v5.6.10 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.11 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.12 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.13 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.14 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.15 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.16 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.17 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.19 conflicts with laravel/framework[v5.5.40].     - don't install illuminate/support v5.6.2|don't install laravel/framework v5.5.40     - illuminate/support v5.6.20 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.21 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.22 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.23 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.24 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.25 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.26 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.27 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.28 conflicts with laravel/framework[v5.5.40].     - don't install illuminate/support v5.6.3|don't install laravel/framework v5.5.40     - illuminate/support v5.6.8 conflicts with laravel/framework[v5.5.40].     - illuminate/support v5.6.9 conflicts with laravel/framework[v5.5.40].     - Installation request for laravel/framework (locked at v5.5.40, required as 5.5.*) -> satisfiable by laravel/framework[v5.5.40].

    Installation failed, reverting ./composer.json to its original content.

    opened by eluotao 8
  • Weird problems to decode/encode

    Weird problems to decode/encode

    Hi, I´m encountering problems to encode/decode the ids of my models. It´s a lot of weird, because it doesn´t always happen.

    I have a few screens to explain my problem:

    1. I create a new document and check in the select, all the id's are encode: 1

    2. When I submit the document, the decode fails: 2

    3. I go back, refresh the page, and the id´s are corrupt: 3

    4. Finally, go to dashboard, go again to create document, check the id´s encode (same as image 1) and now, the decode works well: 4

    I don´t know what it´s happening.... This is all my config and uses:

    • config/hashids.php
    'default' => 'main',
    'connections' => [
    
            'main' => [
                'salt' => 'my-password-secret',
                'length' => '12',
                'alphabet' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890',
            ],
    
            'alternative' => [
                'salt' => 'your-salt-string',
                'length' => 'your-length-integer',
                'alphabet' => 'your-alphabet-string',
            ],
    
        ],
    
    • DocumentController (create method)
    public function create()
        {
                $users = User::select(['id', 'name'])->get();
                foreach ($users as $user) {
                    $owners[Hashids::encode($user->id)] = $user->name;
                } 
                
                return view('documents.create', ['owners' => $owners]);                       
        }
    
    • Document form view
    {{ Form::select('user_id', $owners, null, ['class' => 'form-control']) }}
    
    • Document Store
    public function store(DocumentStore $request)
    {
        dump($request->all());
        dump(Hashids::decode($request->user_id));
        $user_id = Hashids::decode($request->user_id)[0];
    
        $document = new Document();
        $document->fill($request->all());
        $document->user_id = $user_id;
    
        $document->save();
    
        return redirect()->route('documents.index')->with('message', 'Successfully created Document!');
    }
    

    Sorry for the long issue and thanks!!

    opened by Albvadi 8
  • Confusing Config

    Confusing Config

    The config might be better if it was a little more descriptive. I can fill the salt with garbage, I get that but what are the other values supposed to look like. Length? Alphabet? Idk.

        'main' => [
            'salt'     => 'your-salt-string',
            'length'   => 'your-length-integer',
            'alphabet' => 'your-alphabet-string'
        ],
    
    opened by rjksn 8
  • Can not decode after upgrage PHP 7

    Can not decode after upgrage PHP 7

    This just happen to day after I upgrade PHP from 5.6 to 7.0 The encode is working fine but decode seem to be a problem. Could you please guide me where should I look.

    Thanks for the great package you have made.

    opened by oom-patchara 7
  • Add an interface and trait for Eloquent models

    Add an interface and trait for Eloquent models

    These make it a simple process to add methods to Eloquent models to retrieve an instance's hashid, and to find a model based on its hashid.

    I find myself frequently adding these to projects, and it'd be nice if they were bundled with the bridge.

    I haven't written tests, and they are not thoroughly tested. Before doing any of that I want to gauge your interest in accepting this feature.

    opened by tremby 7
  • return [] with decode()

    return [] with decode()

    i try to hash string decode as the following but the return value is empty array [] Note: encode() work well .

    get('/test', function(){ $hash = Hashids::decode('1LLb3b4ck'); return $hash; });

    Any Suggestions ?

    help wanted 
    opened by muhammadatallah 7
  • travis: run coverage just once

    travis: run coverage just once

    Running coverage just once is usually sufficient. Similar PR for Doctrine - see https://github.com/doctrine/doctrine2/pull/1251

    Also, where is this coverage actually used? Looks like the projects is not even activated on Scrutinizer - https://scrutinizer-ci.com/g/vinkla/hashids

    In case it would be active, I'd add also coverage badge to README.md

    opened by TomasVotruba 7
  • Unsupported operand types: int - string

    Unsupported operand types: int - string

    Hi,

    I am using the package with Laravel 9 in a PHP 8 environment. I am getting the following error in Hashid.php line 203:

    Unsupported operand types: int - string

    Here is the stack trace: https://flareapp.io/share/VmeLXOq5#F77

    Is this related with an incompatibility with php 8?

    Best

    opened by allurco 6
  • Hashids causing server crash

    Hashids causing server crash

    Hi. First off, thanks for the package. I've been using it for years. Recently, when Laravel 5.5 came I upgraded and migrated a project over to it. Everything doesn't work until it does and there's an odd roundabout way about this. Let me explain.

    I'm using Laravel as a backend to a React JS frontend connected via the GraphQL Api... The problem is, when I'm trying to encode some things, it causes my server to crash.

    Per se... encoding the ID of a User model. Hashids::encode($user->id) will cause the server to crash(yes the ID exists when I dd it), unless I try and encode some random string first... Hashids::encode('adsadadasd'), make a request to the server, then comment out the random string encoding, then normal encoding will work again.

    If I clear the cache, php artisan cache:clear, the problem returns. I have to encode a random made up string manually, make a request to the server, have it cache, then I can use the same old code as before and it will work perfectly... until the cache is cleared again. Please help.

    Edit: Narrowed the problem down further, when I clear the cache, if I try to encode a string with 2 characters or less, it crashes. I believe that's why the $user->id won't encode, because it's 1 char long. I tried encoding '1', '12', '123', '1234'. It works from '123' going but for it to work for '12' or '1', I have to encode something 3 chars long first.

    opened by niiapa 5
Releases(7.0.0)
Owner
Vincent Klaiber
Vincent Klaiber
Laravel Flysystem - a Flysystem bridge for Laravel.

Laravel Flysystem Laravel Flysystem was created by, and is maintained by Graham Campbell, and is a Flysystem bridge for Laravel. It utilises my Larave

Graham Campbell 489 Dec 22, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
⚡ Laravel Charts — Build charts using laravel. The laravel adapter for Chartisan.

What is laravel charts? Charts is a Laravel library used to create Charts using Chartisan. Chartisan does already have a PHP adapter. However, this li

Erik C. Forés 31 Dec 18, 2022
Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster.

Laravel Kickstart What is Laravel Kickstart? Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster. It com

Sam Rapaport 46 Oct 1, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022
Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Octohook 13 May 16, 2022
Laravel Sanctum support for Laravel Lighthouse

Lighthouse Sanctum Add Laravel Sanctum support to Lighthouse Requirements Installation Usage Login Logout Register Email Verification Forgot Password

Daniël de Wit 43 Dec 21, 2022
Bring Laravel 8's cursor pagination to Laravel 6, 7

Laravel Cursor Paginate for laravel 6,7 Installation You can install the package via composer: composer require vanthao03596/laravel-cursor-paginate U

Pham Thao 9 Nov 10, 2022
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

Ollie Codes 19 Jul 5, 2021
A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Adnane Kadri 49 Jun 22, 2022
Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

null 686 Dec 28, 2022
Laravel Jetstream is a beautifully designed application scaffolding for Laravel.

Laravel Jetstream is a beautifully designed application scaffolding for Laravel. Jetstream provides the perfect starting point for your next Laravel application and includes login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management.

The Laravel Framework 3.5k Jan 8, 2023
Laravel Larex lets you translate your whole Laravel application from a single CSV file.

Laravel Larex Translate Laravel Apps from a CSV File Laravel Larex lets you translate your whole Laravel application from a single CSV file. You can i

Luca Patera 68 Dec 12, 2022
A Laravel package that adds a simple image functionality to any Laravel model

Laraimage A Laravel package that adds a simple image functionality to any Laravel model Introduction Laraimage served four use cases when using images

Hussein Feras 52 Jul 17, 2022
A Laravel extension for using a laravel application on a multi domain setting

Laravel Multi Domain An extension for using Laravel in a multi domain setting Description This package allows a single Laravel installation to work wi

null 658 Jan 6, 2023
Laravel router extension to easily use Laravel's paginator without the query string

?? THIS PACKAGE HAS BEEN ABANDONED ?? We don't use this package anymore in our own projects and cannot justify the time needed to maintain it anymore.

Spatie 307 Sep 23, 2022
Laravel application project as Sheina Online Store backend to be built with Laravel and VueJS

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Boas Aditya Christian 1 Jan 11, 2022
Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models.

Laravel Wrapper for PostgreSQL's Geo-Extension Postgis Features Work with geometry classes instead of arrays. $model->myPoint = new Point(1,2); //lat

Max 340 Jan 6, 2023
laravel - Potion is a pure PHP asset manager for Laravel 5 based off of Assetic.

laravel-potion Potion is a pure PHP asset manager for Laravel based off of Assetic. Description Laravel 5 comes with a great asset manager called Elix

Matthew R. Miller 61 Mar 1, 2022