Download the PHP package deligoez/laravel-model-hashid without Composer

On this page you can find all versions of the php package deligoez/laravel-model-hashid. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-model-hashid

[![Latest Version on Packagist](https://img.shields.io/packagist/v/deligoez/laravel-model-hashid.svg?style=flat-square)](https://packagist.org/packages/deligoez/laravel-model-hashid) [![Total Downloads](https://img.shields.io/packagist/dt/deligoez/laravel-model-hashid.svg?style=flat-square)](https://packagist.org/packages/deligoez/laravel-model-hashid) ![Packagist](https://img.shields.io/packagist/l/deligoez/laravel-model-hashid) [![CI](https://github.com/deligoez/laravel-model-hashid/actions/workflows/ci.yml/badge.svg)](https://github.com/deligoez/laravel-model-hashid/actions/workflows/ci.yml) [![Open Source Love](https://badges.frapsoft.com/os/v3/open-source.svg?v=102)](https://github.com/ellerbrock/open-source-badge/)

Generate, save, and route Stripe-like Hash Ids for your Laravel Eloquent Models.

Hash Ids are short, unique, and non-sequential identifiers that hide database row numbers from users. For more information, visit hashids.org.

For a User model with an id of 1234, you can generate Hash Ids like user_kqYZeLgo.

You have complete control over Hash Id length, prefix, separator, and alphabet. Check out the configuration section for details.

Table of Contents

Requirements

Package PHP Laravel
^4.0 ^8.3 ^11.0, ^12.0, ^13.0
^3.0 ^8.2 ^9.0 - ^11.0
^2.0 ^8.1 ^9.0 - ^12.0
^1.0 ^8.0 ^8.0

Installation

  1. Install via Composer:

  2. Publish the config file:

Usage

Hash Id Generation

Add the HasHashId trait to any Eloquent model:

This gives you hashId and hashIdRaw attributes, plus a keyFromHashId() static method:

Query Builder Functions

All finding-related query builder functions work with Hash Ids:

Route Model Binding (Optional)

Add the HasHashIdRouting trait to enable route model binding with Hash Ids:

Implicit Binding

Explicit Binding

Register a custom model key in your RouteServiceProvider:

Saving Hash Ids to Database (Optional)

Add the SavesHashId trait to automatically persist Hash Ids:

Set the database_column in your configuration file (default: hash_id). You can configure it globally or per model.

Hash Id generation works on the fly -- saving to the database is not required for generation or decoding.

Since Hash Id generation requires an integer model key, saving to the database results in an additional query after model creation.

Generic Hash Ids

Use the HashId utility class to encode and decode integers without a model:

All parameters are optional and fall back to config values.

Blueprint Macro

A hashId() macro is available on the Blueprint class for migrations:

The default column name comes from the database_column config value.

Hash Id Cast

Use HashIdCast to cast attributes in your model:

Serialization

Add the SerializesHashId trait to replace the primary key with the Hash Id in serialized output (arrays and JSON):

This follows the Stripe pattern of exposing Hash Ids in API responses. It respects $hidden and $visible attributes.

Blade Directive

Use the @hashid directive to output a model's Hash Id in Blade templates:

The output is XSS-safe via Laravel's e() helper.

Decrypting Hash Ids in Form Requests

Add the DecryptsHashIds trait to a FormRequest to automatically convert Hash Id inputs to integer keys after validation:

Artisan Commands

Two Artisan commands are available for encoding and decoding Hash Ids:

Validation

Two validation rules are provided for validating Hash Ids in form requests and validators.

ValidHashId — Format Validation (No Database Hit)

Checks if a value is a valid Hash Id format. Optionally validates against a specific model (prefix + decode check).

HashIdExists — Database Existence Check

Checks if a Hash Id corresponds to an existing database record. Equivalent to Laravel's exists rule for Hash Ids.

Both rules throw an InvalidArgumentException if the given model does not use the HasHashId trait.

Hash Id Terminology

A Hash Id consists of three parts:

Part Example Required
Prefix user No
Separator _ No
Raw Hash Id kqYZeLgo Yes

You can generate Hash Ids with or without a prefix. Set prefix_length to 0 in the config to disable prefixes.

Configuration

Publish the config file and customize:

Option Default Description
salt 'your-secret-salt-string' Salt for Hash Id generation. Change this before deploying.
length 13 Raw Hash Id length (excluding prefix and separator)
alphabet 'abcdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ234567890' Characters used in Hash Id generation (min 16 unique chars)
prefix_length 3 Prefix length from class name. -1 for full name, 0 for none
prefix_case 'lower' Prefix case: lower, upper, camel, snake, kebab, title, studly, plural_studly
separator '_' Separator between prefix and raw Hash Id
database_column 'hash_id' Database column name for SavesHashId trait

Model-Specific Configuration

Override any option per model in the model_generators array:

Upgrading

If you are upgrading from v3 to v4, please see the UPGRADE guide for a list of breaking changes.

Testing

This runs the full quality gate: Rector, Pint, PHPStan, and Pest.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-model-hashid with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
ext-mbstring Version *
hashids/hashids Version ^4.1|^5.0
illuminate/contracts Version ^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package deligoez/laravel-model-hashid contains the following files

Loading the files please wait ...