Skip to content

Instantly share code, notes, and snippets.

@kasparsb
Created December 3, 2020 15:00
Show Gist options
  • Save kasparsb/3843ba0c7057214f7f86c1ce793eb39b to your computer and use it in GitHub Desktop.
Save kasparsb/3843ba0c7057214f7f86c1ce793eb39b to your computer and use it in GitHub Desktop.
Create Laravel Eloquent Model with unique field. Unique value is generated random string. Uniqueness is handled by mysql unique index
<?php namespace App\Helpers;
use Str;
use Exception;
function createUniqueModel($modelClass, $fieldName='hash', $data=[], $length=48, $tries=0) {
try {
$model = $modelClass::create(
array_merge(
$data,
[
$fieldName => Str::random($length)
]
)
);
}
catch (\Illuminate\Database\QueryException $e) {
// In case of MySql use errorInfo[1] instead of getCode
if ($e->errorInfo[1] === 1062) {
if ($tries > 500) {
throw new Exception('Can not generate unique model. Field name '.$fieldName);
}
}
else {
throw $e;
}
return createUniqueModel($modelClass, $data, $fieldName, $length, ++$tries);
}
return $model;
}
@kasparsb
Copy link
Author

kasparsb commented Dec 3, 2020

This is helper function. You should create Model with unique index on field. First argument is Model class and second argument is name of unique field.

This functions generate random string and attempts to insert Model into database. In case of success new Model instance is returned. If unique index fails, then function repeats again with new random string. If functions fails 500 times to insert, then it throws error.

If random string length is pretty long, then there is realy low probability for collision

Use it like this:

$account = createUniqueModel(\App\Account::class, 'hash');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment