Dynamically create a hash of an Eloquent model id value to avoid exposing a record's actual database id.
use Netsells\HashModelIds\HashesModelIdsTrait;
class Model extends BaseModel
{
use HashesModelIdsTrait;
}
Use the HashesModelIdsTrait in an Eloquent model to enable the following functionality:
$model->hashed_idModel::whereHashedId($hashedId)Model::whereHashedIds($hashedIds)Route::get('models/{model}', function (Model $model) {
return $model;
});
$url = url("models/$model->hashed_id");
public function rules()
{
return [
'hashed_id' => [
ExistsWithHashedIdRule::make(Model::class)
->where(function ($query) {
$query->where('type', 'test');
})
->where('foo', 'bar'),
],
];
}
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.
Publish the package translations file:
php artisan vendor:publish --tag=hash-model-ids-lang
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.
./vendor/bin/phpunit
How can I help you explore Laravel packages today?