Ad
How I Can Generate The Unique ID In Laravel?
I’m working with my graduation project in Laravel, and want to generate small unique ID "9 char max" ... I don't need UUID because this will generate 36 char which is too long.
Ad
Answer
You can use PHP function like this:
function unique_code($limit)
{
return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
}
echo unique_code(9);
Output looks like:
s5s108dfc
Here the specifications:
- base_convert – Convert a number between arbitrary bases.
- sha1 – Calculate the sha1 hash of a string.
- uniqid – Generate a unique ID.
- mt_rand – Generate a random value via the Mersenne Twister Random Number Generator.
Or in Laravel you can use laravel Str library: just use this:
use Illuminate\Support\Str;
$uniqid = Str::random(9);
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad