Laravel unique Token Generator
// Generate unique Token From Database.
$new_token = $token->Unique($table_name, $column_name, $size);
Require this package, with Composer, in the root directory of your project.
$ composer require dirape/token
Add the service provider to config/app.php
in the providers
array, or if you're using Laravel 5.5, this can be done via the automatic package discovery.
Dirape\Token\TokenServiceProvider::class
If you want you can use the facade. Add the reference in config/app.php
to your aliases array.
'Token'=>\Dirape\Token\Facades\Facade::class
To use new trait token you need to do some changes in the model that contain the token column.
- To use one column token you need to add
use DirapeToken;
in the model . - In database we use default column called
dt_token
to replace with your column name addprotected $DT_Column='column_name';
in the model . - Token settings are set by default to this value
['type' => DT_Unique, 'size' => 40, 'special_chr' => false]
to replace with your custom settings addprotected $DT_settings=['type'=>DT_Unique,'size'=>60,'special_chr'=>false];
in the model . - you should know that we use custom constants for our token type
Const DT_Unique = 'Unique';
Const DT_UniqueNum = 'UniqueNumber';
Const DT_UniqueStr = 'UniqueString';
Const DT_Random = 'Random';
Const DT_RandomNum = 'RandomNumber';
Const DT_RandomStr = 'RandomString';
- after preparing the model to use our trait token in your code you can set the token with your custom column and settings like this
$user=User::first();
$user->setToken();
$user->save();
- you can use your custom settings in
setToken();
function like this
$user=User::first();
$user->setToken(DT_UniqueStr,100,false);
$user->save();
- you can set your custom column in the function
$user=User::first();
$user->setToken(DT_UniqueStr,100,false,'column_name');
$user->save();
- To get model query with token you can use
WithToken()
.$user=User::WithToken()->get();
- To get model query with no tokens you can use flag
false
inWithToken()
$user=User::WithToken(false)->get();
- To use multi column token you need to add
use DirapeMultiToken;
in the model . - Columns settings are not set by default so you need to make your custom settings in the model
protected $DMT_columns=[
'unique_id'=>['type'=>DT_Unique,'size'=>60,'special_chr'=>false],
'unique_uid'=>['type'=>DT_Unique,'size'=>30,'special_chr'=>false],
];
- you should know that we use custom constants for our token type
Const DT_Unique = 'Unique'; Const DT_UniqueNum = 'UniqueNumber'; Const DT_UniqueStr = 'UniqueString'; Const DT_Random = 'Random'; Const DT_RandomNum = 'RandomNumber'; Const DT_RandomStr = 'RandomString';
- after preparing the model to use our trait multi token in your code you can set the tokens with only one function
$user=User::first(); $user->setTokens(); $user->save();
With this package you can generate unqiue token not repated in database just by using unique($table_name,$column_name,$size)
Function $table_name
is the table name in database , $column_name
is the column name in the table, $size
is token size.
generate unique strings token with the same signature of unique token with function UniqueString($table_name,$column_name,$size)
.
generate unique integers token with the same signature of unique token with function UniqueNumber($table_name,$column_name,$size)
.
generate random token with function Random($size)
and $size
is the size of token length.
generate random integer token with function RandomNumber($size)
and $size
is the size of token length.
generate random string token with function RandomString($size)
and $size
is the size of token length.
use true
to allow special characters in your token !@#$%^&*()
in all functions just like Random($size,true)
.
Here you can see an example of just how simple this package is to use.
// Generate unique token not rebeated in database table with column name
Token::Unique($table_name, $column_name, 10 );
//Result: fCWih6TDAf
// Generate unique integer token not rebeated in database table with column name
Token::UniqueNumber($table_name, $column_name, 10 );
//Result: 9647307239
// Generate unique string token not rebeated in database table with column name
Token::UniqueString($table_name, $column_name, 10 );
//Result: SOUjkyAyxC
//You can use special characters just add "true" to the function
Token::Unique($table_name, $column_name, 10,true );
//Result: H@klU$u^3z
$size=10;
// Generate random token
Token::Random($size);
// Generate random integer token
Token::RandomNumber($size);
// Generate random string token
Token::RandomString($size);
//You can use special characters just add "true" to the function
Token::Random($size,true);