jeudi 5 juillet 2018

Random String Generator that does not repeat in laravel controller

I created this function in my laravel controller.

function incrementalHash($len = 5){
    $charset = "0123456789abcdefghijklmnopqrstuvwxyz";
    $base = strlen($charset);
    $result = '';

    $now = explode(' ', microtime())[1];
    while ($now >= $base){
        $i = $now % $base;
        $result = $charset[$i] . $result;
        $now /= $base;
    }
    return substr($result, -5);
}

then I have a function to insert something in the database. this function uses the above function. but every time I use it I get the same result from above function. I tried composer dump-autoload and the result changes. I wonder what is happening? why this method always returns the same result. how can I use this method and not receive the same result without dumping autoload? here is my controller:

public function add_user_create()
{
    $user = new User;
    $user->user_id = Request()->input('user_id');
    $user->user_name = Request()->input('user_name');
    $user->fcm = Request()->input('fcm');
    $user->email = Request()->input('email');
    $user->token = Request()->input('token');
    $user->profile_pic = Request()->input('profile_pic');
    $user->api_token = str_random(60);
    $user->ref_ID = $this->incrementalHash(4);
    $user->save();
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire