PHP code example of ibrahem-kamal / laravel-otp

1. Go to this page and download the library: Download ibrahem-kamal/laravel-otp 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/ */

    

ibrahem-kamal / laravel-otp example snippets


return [
    'services' => [
        'default' => [
            'expires_in' => 5, // in minutes
            'otp_generator_options' => [
                'length' => 4, // no of digits
                'numbers' => true,
                'letters' => false,
                'symbols' => false,
            ],
            'validate_uniqueness_after_generation' => true,
            'delete_after_verification' => false,
        ],
    ],
    'fallback_options' => [
        'otp_generator_options' => [
            'length' => 4, // no of digits
            'numbers' => true,
            'letters' => false,
            'symbols' => false,
        ],
        'validate_uniqueness_after_generation' => true, // whether to validate the uniqueness of the otp after generation by checking the database
        'delete_after_verification' => false, // whether to delete the otp after verification
    ]

];

class User extends Authenticatable implements HasOtp
{
    use InteractsWithOtp;
}

    public function getPhoneNumber(): string
    {
        return $this->mobile_number;
    }

$user->otp()->generate() // returns OtpCode Model instance

// you can also pass the service name to generate otp for a specific service or modify the options

$user->otp()
        ->setPhone('11111')
        ->setValidateUniquenessAfterGeneration(false)
        ->setService('other service')
        ->setGeneratorOptions(
            length: 6,
            letters: false,
            numbers: true, 
            symbols: false
        )->generate()  // returns OtpCode Model instance

$otp = $user->otp()->verifyOtp('1234') // returns ServiceResponse instance
    $otp->isSuccess(); //bool
    $otp->getErrorsString(); // errors as string
    $otp->getErrors(); // errors as array
    $otp->getData(); // OtpCode Model instance when success
    $otp->toArray(); // array of all the above
bash
php artisan vendor:publish --tag="laravel-otp-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-otp-config"