PHP code example of hith / laravel-codeable

1. Go to this page and download the library: Download hith/laravel-codeable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hith / laravel-codeable example snippets


use Codeable\Traits\HasCodes;

class User extends Model
{
    use HasCodes;
}


$user->createCode();
$code = $user->createCode(type:'code_type', length:6, timeToExpire:'15:m'); // or define the attributes you want
$user->deleteCode($code); // delete by passing a Code model instance
$user->deleteCode('code_type'); // delete by code type
$user->deleteCode($code->id); // delete by code id

use Codeable\Codeable;

$codeable = new Codeable();
$codeable->createCode();
$code = $codeable->createCode(type:'code_type', length:6, timeToExpire:'15:m');
$codeable->deleteCode($code);
$codeable->deleteCode('code_type');


use Codeable\Facades\Coder;

$code = Coder::createCode();
if(! $code->isValid()){
    Coder::delete($code);
}

$code = Coder::codeByType('code_type');
if($code->isExpired()){
    Coder::delete($code);
}


// Get a code by type or by code value
$user->code('code_type');
$user->code(12345);

// Or use explicit methods
$user->codeByType('code_type');
$user->codeByCode(12345);

// Or fetch it by ID
$user->codeById($code->id);


/**
 *  min_length   : int
 *  The minimum number of digits allowed in a generated code.
 * */
'min_length' => 3,

/**
 *  max_length   : int
 *  The maximum number of digits allowed in a generated code.
 * */
'max_length' => 16,

/**
 *  max_attempts : int
 *  The maximum number of attempts to generate a unique code before failing.
 *  Example: 5 → after 5 unsuccessful tries, any code will be returned.
 * */
'max_attempts' => 1,

/**
 *   valid_units  : string[]
 *   Allowed time units for setting the `expire_at` field in the database record.
 */
'valid_units' => [
    's' => 'second',
    'm' => 'minute',
    'h' => 'hour',
    'd' => 'day',
]

bash
php artisan vendor:publish --tag=codeable-migrations
php artisan vendor:publish --tag=codeable-command
php artisan vendor:publish --tag=codeable-config
bash
php artisan vendor:publish --tag=codeable-files

bash
php artisan codes:delete-expired