In terms of token generation, you can use one of the Larvel Helper Functions ; str_random() .
This will create a random string of a certain length, for example, str_random(16) will create a random string of 16 characters (uppercase, lowercase and numbers).
Depending on how you use the tokens, should they really be completely unique? Given that they will match the user, or I assume you can use token_id and then check this for token_key , does it really matter if there is one of them? - although the chances of it are extremely small!
However, if you really need to be unique, you can always use a validator with a unique constraint. Using this package , you can also verify that both of them are unique also with unique_with . And then, if the validator fails, it generates a new token as necessary.
Based on your examples, you use str_random(16) for token_id and str_random(30) for token_key .
James source share