Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Hash Model Ids Laravel Package

netsells/hash-model-ids

View on GitHub
Deep Wiki
Context7

Hash Model Ids

Dynamically create a hash of an Eloquent model id value to avoid exposing a record's actual database id.

Features

use Netsells\HashModelIds\HashesModelIdsTrait;

class Model extends BaseModel
{
    use HashesModelIdsTrait;
}

Use the HashesModelIdsTrait in an Eloquent model to enable the following functionality:

  • Access a model's hashed id value: $model->hashed_id
  • Filter a model by an array of or a single hashed id:
    • Model::whereHashedId($hashedId)
    • Model::whereHashedIds($hashedIds)
  • Define routes with models bound by their hashed id:
    Route::get('models/{model}', function (Model $model) {
        return $model;
    });
    
    
    $url = url("models/$model->hashed_id");
    
  • Check for model existence in form request classes using a bespoke rule, optionally including additional chained constraints:
    public function rules()
    {
        return [
            'hashed_id' => [
                ExistsWithHashedIdRule::make(Model::class)
                    ->where(function ($query) {
                        $query->where('type', 'test');
                    })
                    ->where('foo', 'bar'),
            ],
        ];
    }
    

Installation

Install the package with:

composer require netsells/hash-model-ids

Publish the package config file:

php artisan vendor:publish --tag=hash-model-ids-config

Optionally set a HASH_MODEL_IDS_SALT in .env.

Translations

Publish the package translations file:

php artisan vendor:publish --tag=hash-model-ids-lang

Development

Sometimes, during development, it can be awkward dealing with hashed ids. Set HASH_MODEL_IDS_ENABLED=false in your environment file to enable use of a (configurable) prefixed version of a model's actual id rather than the default hashed version.

Testing

./vendor/bin/phpunit

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
adachsoft/php-code-style