hexify/laravel-id-customizer
Laravel package for generating custom IDs for models or any table column. Create prefixed incremental IDs (with optional reset on prefix change) or random IDs with configurable length, character set, and extras. Use via a controller helper or a model trait.
Installation:
composer require hexify/laravel-id-customizer
Publish the config file (if needed):
php artisan vendor:publish --provider="Hexify\LaraIdCustomizer\LaraIdCustomizerServiceProvider"
First Use Case: Generate a custom ID for a model in a controller:
use Hexify\LaraIdCustomizer\IdCustomizer;
$uid = IdCustomizer::generate([
'model' => Student::class,
'column' => 'uid',
'length' => 10,
'prefix' => 'STU'
]);
Usage section for basic syntax.config/laravel-id-customizer.php for default settings.HasIdFactory and IdFactory for model integration.Workflow:
$config = [
'model' => User::class,
'column' => 'custom_id',
'length' => 12,
'prefix' => 'USER',
'suffix' => date('Y'),
'padding' => '0' // Optional: Pad with zeros
];
$customId = IdCustomizer::generate($config);
$user = new User(['custom_id' => $customId, ...]);
Integration Tips:
store() or create() methods for consistent ID formatting.Workflow:
IdFactory and use HasIdFactory in your model:
use Hexify\LaraIdCustomizer\IdFactory;
use Hexify\LaraIdCustomizer\Traits\HasIdFactory;
class Product extends Model implements IdFactory {
use HasIdFactory;
protected $idConfig = [
'column' => 'sku',
'length' => 8,
'prefix' => 'PROD',
'suffix' => 'AUTO'
];
}
getCustomId() to customize logic:
public function getCustomId(): string
{
return $this->generateId([
'prefix' => 'PREMIUM_' . $this->category,
'length' => 10
]);
}
Common Patterns:
department) in prefix:
'prefix' => 'DEPT_' . $this->departmentCode
boot() method to validate IDs before saving:
protected static function boot()
{
parent::boot();
static::saving(function ($model) {
if (!$model->isValidCustomId($model->{$model->getIdColumn()})) {
throw new \Exception('Invalid ID format');
}
});
}
Column Mismatch:
column in $idConfig matches a fillable field in your model. Non-fillable fields will silently fail.$fillable:
protected $fillable = ['id', 'custom_id', ...];
ID Collisions:
length + prefix/suffix exceeds column size (e.g., VARCHAR(10)), Laravel will truncate silently.boot():
static::creating(function ($model) {
$id = $model->generateId($model->getIdConfig());
if (strlen($id) > $model->{$model->getIdColumn() . '_length}) {
throw new \Exception('ID exceeds column length');
}
});
Trait Conflicts:
boot() methods, ensure HasIdFactory runs last.booted() instead of boot().$idConfig:
\Log::debug('ID Config:', $this->getIdConfig());
length = 0 (should return only prefix/suffix).prefix/suffix longer than length (behavior may vary).Custom Generators:
IdCustomizer class to add new formats (e.g., UUID, hashed IDs):
class CustomIdCustomizer extends IdCustomizer {
public static function generateUuid(): string {
return Str::uuid()->toString();
}
}
Dynamic Configs:
$idConfig from a database or API:
$this->idConfig = Setting::where('key', 'product_sku_format')->first()->value;
Event Hooks:
event(new CustomIdGenerated($this, $this->custom_id));
Default Values: The package uses these defaults if not specified:
[
'length' => 10,
'prefix' => '',
'suffix' => '',
'padding' => '0'
]
Override them in config/laravel-id-customizer.php for global changes.
Case Sensitivity:
Prefixes/suffixes are case-sensitive. Use strtoupper() if consistency is needed:
'prefix' => strtoupper(date('ym'))
How can I help you explore Laravel packages today?