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



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

class Post extends Model implements Hashable
{
    use TraitsHasHashId;

    protected $fillable = [
        'title',
    ];

    function getHashAttributes(): array
    {
        $pattern = config('hashid.hashed_attributed_pattern', '%s_hashed');
        return [
            // return mixed alphabet encrypted key, also text & int available
            'id' => HashBuilder::mixed('post_key')
                ->minLength(5)
                ->prefix('PK-')
                ->suffix(function (self $model) {
                    return $model->owner->isActive ? '-KD' : '-KP';
                })
        ];
    }
}