PHP code example of antoninmasek / laravel-model-hashids

1. Go to this page and download the library: Download antoninmasek/laravel-model-hashids 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/ */

    

antoninmasek / laravel-model-hashids example snippets


return [
    /*
     * The following column will be filled with the generated hash id. If you decide to also bind
     * models to hash_id, then this column will be used as route key name.
     */
    'hash_id_column' => 'hash_id',

    /*
     * Define the column name, which will be used to generate the hash id. This column has to contain
     * a numeric value or an array of numbers and should be unique for each model to prevent
     * potential collisions.
     */
    'model_key' => 'id',
];

use AntoninMasek\Hashids\Traits\GeneratesHashId;

class YourModel extend Model
{
    use GeneratesHashId;
}

use AntoninMasek\Hashids\Traits\GeneratesHashId;
use AntoninMasek\Hashids\Traits\BindsOnHashId;

class YourModel extend Model
{
    use GeneratesHashId;
    use BindsOnHashId;
}

use AntoninMasek\Hashids\ModelHashids;

ModelHashids::generateSaltUsing(function(Model $model) {
    // your logic   
    return $salt;
});

ModelHashids::generateMinLengthUsing(function(Model $model) {
    // your logic   
    return $minLength;
});

ModelHashids::generateAlphabetUsing(function(Model $model) {
    // your logic   
    return $alphabet;
});

// Overwrite the column to fill with hash id for this specific model
public function hashIdColumn(): string;

// Overwrite the column to use for hash id generation for this specific model
public function hashIdKeyColumn(): string;

// Overwrite the logic to generate salt for this specific model
public function hashIdSalt(): string;

// Overwrite the logic to generate alphabet for this specific model
public function hashIdAlphabet(): string;

// Overwrite the logic to generate min length for this specific model
public function hashIdMinLength(): string;

// This will save the new hash id directly to database
$model->regenerateHashId();

// This will just regenerate the hash id on the instance without persisting it.
// You will need to call the save() method to persist it.
$model->regenerateHashId(saveToDatabase: false);
bash
php artisan vendor:publish --tag="laravel-model-hashids-config"