PHP code example of mcris112 / laravel-hashidable

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

    

mcris112 / laravel-hashidable example snippets


use Mcris112\LaravelHashidable\Hashidable;

Class User extends Model
{
  use Hashidable;
}

$user = User::find(1);

$user->id; // 1
$user->hashid; // 3RwQaeoOR1E7qjYy

User::find(1);

// User::findByHashId( string|array $hashId, array $columns ); Returns a model or a collection of models
User::findByHashId('3RwQaeoOR1E7qjYy');
User::findByHashidOrFail('3RwQaeoOR1E7qjYy');

User::whereHashid('3RwQaeoOR1E7qjYy')->first();

User::hashIdDecode('3RwQaeoOR1E7qjYy'); //Returns the hash decoded,
User::hashIdDecode(['3RwQaeoOR1E7qjYy', ...$hashes]); //This also can be as array


  User::whereHashid( string $hashid , string $columnId = 'id' )->first();

  //Example

  public function show(string $userId, Request $request)
  {
    // ...
      $user = User::whereHashid($userId)->where('is_email_verified', true)->first();
      // ...
  }


Route::apiResource('users', UserController::class);

public function show(Request $request, User $user)
{
  return $user; // Works just fine
}

return [
    /**
     * Length of the generated hashid.
     */
    'length' => 16,

    /**
     * Character set used to generate the hashids.
     */
    'charset' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',

    /**
     * Prefix attached to the generated hash.
     */
    'prefix' => '',

    /**
     * Suffix attached to the generated hash.
     */
    'suffix' => '',

    /**
     * If a prefix of suffix is defined, we use this as a separator
     * between the prefix/suffix.
     */
    'separator' => '-',
];

    public function hashidableConfig()
    {
        return ['prefix' => 'app'];
    }

php artisan vendor:publish --tag=hashidable.config