PHP code example of luilliarcec / laravel-username-generator

1. Go to this page and download the library: Download luilliarcec/laravel-username-generator 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/ */

    

luilliarcec / laravel-username-generator example snippets


use Illuminate\Database\Eloquent\Model;
use Luilliarcec\LaravelUsernameGenerator\Concerns\HasUsername;

class User extends Model
{
    use HasUsername;
}

protected function getUsernameColumn(): string
{
    return 'my_username_column';
}

protected function getName(): string
{
    // This is the value, not the field name.
    return $this->name;
}

protected function getLastName(): ?string
{
    // This is the value, not the field name.
    return $this->last_name;
}

use Luilliarcec\LaravelUsernameGenerator\Drivers\Email;

protected function getUsernameDriver(): DriverContract
{
    return new Email();
}

protected function transformUsername(string $username): string
{
    return mb_strtoupper($username, 'UTF-8');
}

namespace App\Support\Username\Drivers;

use Luilliarcec\LaravelUsernameGenerator\Contracts\DriverContract;

class CustomDriver implements DriverContract
{
    public function make(string $name, string $lastname = null): string
    {
        // your code
    }
}

$model = User::create(['name' => 'Luis Andrés Arce Cárdenas']);

$model->username; // larcec

$model = User::create(['name' => 'Luciano Carlos Arce Cajamarca']);

$model->username; // larcec1

$model = User::create(['first_name' => 'Luis Andrés', 'last_name' => 'Arce Cárdenas']);
$model = User::create(['name' => 'Luis Andrés Arce Cárdenas']);
// This will generate the following username: larcec

$model = User::create(['first_name' => 'Luis', 'last_name' => 'Arce']);
$model = User::create(['name' => 'Luis Arce']);
// This will generate the following username: larce

$model = User::create(['first_name' => 'Luis', 'last_name' => 'Arce Cárdenas']);
$model = User::create(['name' => 'Luis Arce Cárdenas']);
// This will generate the following username: larcec

$model = User::create(['email' => '[email protected]']);
// This will generate the following username: luilliarcec