PHP code example of m-wardany / hash-ids

1. Go to this page and download the library: Download m-wardany/hash-ids library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

m-wardany / hash-ids example snippets


return [
    // Default encryption key (can be overridden per attribute)
    'encryption_key' => env('HASHID_ENCRYPTION_KEY', env('APP_KEY')),
    
    // Pattern for hashed attribute names (e.g., 'id' becomes 'id_hashed')
    'hashed_attributed_pattern' => '%s_hashed',
    
    // Default minimum length for hashed values
    'min_length' => 5,
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use MWardany\HashIds\Helpers\HashBuilder;
use MWardany\HashIds\Interfaces\Hashable;
use MWardany\HashIds\Traits\HasHashId;

class Post extends Model implements Hashable
{
    use HasHashId;

    protected $fillable = ['title', 'ref'];

    public function getHashAttributes(): array
    {
        return [
            'id' => HashBuilder::mixed('post_key')
                ->minLength(5)
                ->prefix('PK-'),
        ];
    }
}

HashBuilder::mixed('hashed_attribute_name')

HashBuilder::text('hashed_attribute_name')

HashBuilder::int('hashed_attribute_name')

HashBuilder::mixed('post_key')->minLength(10)

HashBuilder::mixed('post_key')->prefix('POST-')

HashBuilder::mixed('post_key')->suffix('-ACTIVE')

HashBuilder::mixed('post_key')->suffix(function (Post $model) {
    return $model->is_active ? '-ACTIVE' : '-INACTIVE';
})

HashBuilder::mixed('post_key')->encryptionKey('custom_secret_key')

public function getHashAttributes(): array
{
    return [
        'id' => HashBuilder::mixed('post_key')
            ->minLength(8)
            ->prefix('POST-')
            ->suffix(function (self $model) {
                return $model->owner->isActive ? '-ACTIVE' : '-INACTIVE';
            }),
    ];
}

public function getHashAttributes(): array
{
    return [
        'ref' => [
            HashBuilder::mixed('primary_hash')
                ->minLength(6)
                ->prefix('PRI-')
                ->encryptionKey('primary_key'),
            
            HashBuilder::text('secondary_hash')
                ->minLength(8)
                ->prefix('SEC-')
                ->encryptionKey('secondary_key'),
        ],
    ];
}

public function getHashAttributes(): array
{
    return [
        'id' => HashBuilder::mixed('public_id')
            ->minLength(10)
            ->prefix('POST-'),
        
        'user_id' => HashBuilder::int('user_ref')
            ->minLength(8)
            ->prefix('USER-'),
        
        'reference' => [
            HashBuilder::mixed('internal_ref')
                ->minLength(12)
                ->encryptionKey(config('app.internal_key')),
            
            HashBuilder::text('external_ref')
                ->minLength(10)
                ->prefix('EXT-')
                ->encryptionKey(config('app.external_key')),
        ],
    ];
}

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('post_key')->nullable()->unique(); // Hashed ID column
    $table->timestamps();
});
bash
php artisan vendor:publish --provider="MWardany\HashIds\Providers\ServiceProvider"