Created
December 3, 2020 15:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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');