Convert Figma logo to code with AI

vinkla logolaravel-hashids

A Hashids bridge for Laravel

1,981
173
1,981
0

Top Related Projects

An opinionated package to create slugs for Eloquent models

Easy creation of slugs for your Eloquent models in Laravel

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

2,745

Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.

11,274

🔐 JSON Web Token Authentication for Laravel & Lumen

Associate users with roles and permissions

Quick Overview

The vinkla/laravel-hashids package is a Laravel wrapper for the Hashids library, which is a small open-source library that generates short, unique, non-sequential ids from numbers. This package provides a convenient way to use Hashids within a Laravel application.

Pros

  • Easy Integration: The package is designed to integrate seamlessly with Laravel, providing a simple and intuitive API for working with Hashids.
  • Flexible Configuration: The package allows you to configure various Hashids settings, such as the salt, minimum hash length, and alphabet, to suit your application's needs.
  • Dependency Management: By using this package, you can avoid the need to manage the Hashids library directly, as it is handled by the package.
  • Testability: The package includes a set of unit tests, ensuring the reliability and stability of the provided functionality.

Cons

  • Limited Functionality: The package primarily focuses on providing a Laravel-specific wrapper for the Hashids library, and does not offer any additional features beyond the core Hashids functionality.
  • Dependency on Hashids Library: The package's functionality is entirely dependent on the Hashids library, and any limitations or issues with the Hashids library may be reflected in the package.
  • Potential Overhead: Depending on the complexity of your application, the additional layer of abstraction provided by the package may introduce a small amount of overhead compared to directly using the Hashids library.
  • Lack of Extensive Documentation: While the package's documentation is generally clear and concise, it may not provide in-depth guidance for more advanced use cases or edge cases.

Code Examples

Here are a few examples of how to use the vinkla/laravel-hashids package:

  1. Encoding a number:
use Vinkla\Hashids\Facades\Hashids;

$id = Hashids::encode(1234);
// Output: "aBMoq8"
  1. Decoding a hash:
$numbers = Hashids::decode('aBMoq8');
// Output: [1234]
  1. Configuring the Hashids instance:
// In your config/hashids.php file
return [
    'salt' => 'your-salt-string',
    'length' => 8,
    'alphabet' => 'abcdefghijklmnopqrstuvwxyz',
];

// In your code
$id = Hashids::encode(1234);
// Output: "aBMoq8Xr"
  1. Using the Hashids instance directly:
use Vinkla\Hashids\Hashids;

$hashids = new Hashids('your-salt-string', 8, 'abcdefghijklmnopqrstuvwxyz');
$id = $hashids->encode(1234);
// Output: "aBMoq8Xr"

Getting Started

To get started with the vinkla/laravel-hashids package, follow these steps:

  1. Install the package using Composer:
composer require vinkla/laravel-hashids
  1. (Optional) Publish the package's configuration file:
php artisan vendor:publish --provider="Vinkla\Hashids\HashidsServiceProvider"

This will create a config/hashids.php file, where you can customize the Hashids settings.

  1. Use the Hashids facade in your code to interact with the Hashids library:
use Vinkla\Hashids\Facades\Hashids;

$id = Hashids::encode(1234);
$numbers = Hashids::decode('aBMoq8');

Alternatively, you can inject the Hashids instance directly into your classes:

use Vinkla\Hashids\Hashids;

class MyClass
{
    private $hashids;

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

    public function example()
    {
        $id = $this

Competitor Comparisons

An opinionated package to create slugs for Eloquent models

Pros of Laravel Sluggable

  • Generates SEO-friendly slugs for better URL readability
  • Supports multiple languages and custom slug generators
  • Offers automatic slug generation on model creation or update

Cons of Laravel Sluggable

  • Limited to slug generation, not suitable for general ID obfuscation
  • May require additional configuration for complex slug requirements
  • Potential performance impact on large datasets due to uniqueness checks

Code Comparison

Laravel Sluggable:

use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;

class Post extends Model
{
    use HasSlug;

    public function getSlugOptions() : SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('title')
            ->saveSlugsTo('slug');
    }
}

Laravel Hashids:

use Vinkla\Hashids\Facades\Hashids;

$id = 1;
$hash = Hashids::encode($id);
$decodedId = Hashids::decode($hash)[0];

Laravel Sluggable focuses on creating human-readable slugs for URLs, while Laravel Hashids is designed for ID obfuscation. Sluggable is more suitable for content-based models, whereas Hashids is better for general ID encoding and decoding across various model types.

Easy creation of slugs for your Eloquent models in Laravel

Pros of eloquent-sluggable

  • Offers more customization options for slug generation
  • Supports multiple slug sources and custom slug generators
  • Provides built-in handling for duplicate slugs

Cons of eloquent-sluggable

  • More complex setup and configuration compared to laravel-hashids
  • May have a slight performance impact due to additional database queries for uniqueness checks

Code Comparison

eloquent-sluggable:

use Cviebrock\EloquentSluggable\Sluggable;

class Post extends Model
{
    use Sluggable;

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

laravel-hashids:

use Vinkla\Hashids\Facades\Hashids;

$id = 1;
$hashid = Hashids::encode($id);
$decodedId = Hashids::decode($hashid)[0];

eloquent-sluggable is more focused on generating human-readable slugs for URLs, while laravel-hashids is designed for encoding and decoding numeric IDs. eloquent-sluggable offers more flexibility in slug generation but requires more setup. laravel-hashids is simpler to use but limited to ID encoding. The choice depends on whether you need user-friendly URLs (eloquent-sluggable) or obfuscated IDs (laravel-hashids).

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Pros of laravel-mongodb

  • Provides full MongoDB integration for Laravel, including Eloquent model support
  • Offers advanced features like geospatial queries and aggregation pipelines
  • Allows seamless use of MongoDB alongside traditional SQL databases in Laravel projects

Cons of laravel-mongodb

  • More complex setup and configuration compared to the simpler Hashids integration
  • Requires learning MongoDB syntax and concepts, which may be unfamiliar to some developers
  • Potential performance overhead for simple use cases where Hashids would suffice

Code Comparison

laravel-mongodb:

use Jenssegers\Mongodb\Eloquent\Model;

class User extends Model
{
    protected $connection = 'mongodb';
}

laravel-hashids:

use Vinkla\Hashids\Facades\Hashids;

$id = Hashids::encode(1, 2, 3);
$numbers = Hashids::decode($id);

Summary

laravel-mongodb is a comprehensive solution for integrating MongoDB with Laravel, offering powerful features and flexibility. It's ideal for projects requiring NoSQL database capabilities or working with large, unstructured datasets. However, it comes with a steeper learning curve and more complex setup.

laravel-hashids, on the other hand, is a simpler package focused on generating short, unique IDs from integers. It's easier to implement and use, making it suitable for basic ID obfuscation or creating short URLs. However, it lacks the advanced database features provided by laravel-mongodb.

Choose based on your project's specific needs: laravel-mongodb for full NoSQL integration, or laravel-hashids for simple ID encoding/decoding functionality.

2,745

Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.

Pros of Sanctum

  • Built-in authentication system for SPAs, mobile apps, and token-based APIs
  • Seamless integration with Laravel's ecosystem and authentication features
  • Lightweight and easy to implement for Laravel applications

Cons of Sanctum

  • Limited to Laravel applications, not as flexible for other frameworks
  • May be overkill for simple projects that only need basic ID obfuscation

Code Comparison

Sanctum (API token creation):

$token = $user->createToken('token-name');
return $token->plainTextToken;

Laravel-hashids (ID encoding):

use Vinkla\Hashids\Facades\Hashids;

$id = Hashids::encode(1, 2, 3);
$numbers = Hashids::decode($id);

Key Differences

  • Sanctum focuses on authentication and API token management
  • Laravel-hashids is primarily for ID obfuscation and encoding
  • Sanctum is an official Laravel package, while Laravel-hashids is a third-party solution

Use Cases

  • Sanctum: Best for Laravel applications requiring robust API authentication
  • Laravel-hashids: Ideal for projects needing simple ID obfuscation or short URL generation

Integration

  • Sanctum integrates deeply with Laravel's authentication system
  • Laravel-hashids can be used in any PHP project, not limited to Laravel

Community and Support

  • Sanctum has strong official support from the Laravel team
  • Laravel-hashids relies on community support and third-party maintenance
11,274

🔐 JSON Web Token Authentication for Laravel & Lumen

Pros of jwt-auth

  • Provides robust JSON Web Token (JWT) authentication for Laravel applications
  • Offers more advanced security features like token refresh and blacklisting
  • Suitable for stateless authentication in API-driven applications

Cons of jwt-auth

  • More complex setup and configuration compared to laravel-hashids
  • Requires additional server-side processing for token validation and management
  • May have a steeper learning curve for developers new to JWT authentication

Code Comparison

laravel-hashids:

use Vinkla\Hashids\Facades\Hashids;

$id = Hashids::encode(1, 2, 3);
$numbers = Hashids::decode($id);

jwt-auth:

$token = JWTAuth::attempt($credentials);
$user = JWTAuth::toUser($token);
JWTAuth::invalidate($token);

Summary

laravel-hashids is a simpler solution for generating short, unique IDs from integers, while jwt-auth provides a comprehensive JWT authentication system for Laravel. laravel-hashids is easier to implement but offers less security features, whereas jwt-auth is more complex but provides robust authentication capabilities for API-driven applications.

Associate users with roles and permissions

Pros of laravel-permission

  • Comprehensive role-based access control system
  • Supports both roles and permissions for fine-grained access control
  • Includes database migrations and model relationships out of the box

Cons of laravel-permission

  • More complex setup and configuration compared to laravel-hashids
  • Potentially higher performance overhead due to database queries for permission checks

Code Comparison

laravel-permission:

$user->assignRole('writer');
$user->givePermissionTo('edit articles');

if ($user->hasPermissionTo('edit articles')) {
    // User can edit articles
}

laravel-hashids:

$id = Hashids::encode(1, 2, 3);
$numbers = Hashids::decode($id);

$model = Model::findOrFail(Hashids::decode($id)[0]);

Summary

laravel-permission is a comprehensive role and permission management system for Laravel applications, offering fine-grained access control. It's more complex but provides robust functionality for managing user roles and permissions.

laravel-hashids, on the other hand, is a simpler package focused on generating short, unique IDs from numbers. It's useful for creating obfuscated identifiers but doesn't provide any access control features.

Choose laravel-permission for complex authorization needs, and laravel-hashids for simple ID obfuscation tasks.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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.