1. Go to this page and download the library: Download veelasky/laravel-hashid 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/ */
veelasky / laravel-hashid example snippets
class User extends Model
{
use HashableId;
// getRouteKey() automatically returns hash - no implementation needed!
}
route('users.show', $user); // Automatically generates: /users/k1jTdv6l
class User extends Model
{
use HashableId;
// Default: only hash resolution, numeric IDs return 404
}
// ✅ Secure: /users/k1jTdv6l works
// ❌ Blocked: /users/1 returns 404 (prevents ID enumeration)
// Get user by hash with specific columns (better performance!)
$user = User::byHash($hash, ['name', 'email']);
// Get user by hash with single column
$user = User::byHash($hash, ['name']);
// Column selection with exception handling
$user = User::byHashOrFail($hash, ['name', 'email']);
use Illuminate\Database\Eloquent\Model;
use Veelasky\LaravelHashId\Eloquent\HashableId;
class User extends Model
{
use HashableId;
}
$user = User::find(1); // Find user by ID
$user->hash; // Get HashId automatically
// Find by HashId
$user = User::byHash($hash);
$user = User::byHashOrFail($hash); // Throws exception if not found
// Convert between ID and HashId
$hashedId = User::idToHash($id);
$originalId = User::hashToId($hash);
// Query scope
User::query()->byHash($hash)->get();
// Load only specific columns for better performance
$user = User::byHash($hash, ['name', 'email']);
// Single column selection
$user = User::byHash($hash, ['name']);
// Column selection with exception handling
$user = User::byHashOrFail($hash, ['name', 'email']);
class User extends Model
{
use HashableId;
protected $shouldHashPersist = true; // Persist hash to database
protected $hashColumnName = 'hashid'; // Custom column name (optional)
}
Route::get('/users/{user}', [UserController::class, 'show']);
class UserController
{
public function show(User $user)
{
// $user resolves automatically by HashId
// Example URL: /users/k1jTdv6l
// Numeric IDs like /users/1 will return 404 by default (secure!)
}
}
class User extends Model
{
use HashableId;
// $bindingFallback = false; // Default behavior - only hash resolution
}
// ✅ This works: /users/k1jTdv6l
// ❌ This returns 404: /users/1 (prevents ID enumeration)
class User extends Model
{
use HashableId;
protected $bindingFallback = true; // Allow both hash and numeric ID resolution
}
// ✅ Both work: /users/k1jTdv6l AND /users/1
Route::get('/users/{user:slug}', [UserController::class, 'show']);
// This will resolve by 'slug' field, not by hash
class User extends Model
{
use HashableId;
// No manual getRouteKey() implementation needed!
}
// Route generation now automatically uses hash
route('users.show', $user); // Generates: /users/k1jTdv6l
class User extends Model
{
use HashableId;
// Manual implementation was
use App\Models\User;
use Veelasky\LaravelHashId\Rules\ExistsByHash;
$request->validate([
'user_id' => ['
// Using the HashId facade
$hashedId = HashId::idToHash($id, User::class);
$originalId = HashId::hashToId($hash, User::class);
// Manual hash ID creation
$hashId = HashId::make('custom-key', 'custom-salt');
class User extends Model
{
protected $hashKey = 'shared-hash-key';
}
class Customer extends User { }
$customer = Customer::find(1);
$user = User::find(1);
// Both will have the same hash
echo $customer->hash === $user->hash; // true