PHP code example of shabushabu / laravel-uid

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

    

shabushabu / laravel-uid example snippets


return [
    // ...
    'prefixes' => [
        'usr' => \App\Models\User::class,
    ],
];

use ShabuShabu\Uid\Concerns\HasUid;
use ShabuShabu\Uid\Contracts\Identifiable;

class User extends Model implements Identifiable
{
    use HasUid;
}

return [
    // ...
    'alphabets' => [
        'usr' => 'abcdefgh...'
    ],
];

use ShabuShabu\Uid\Service\Uid;

$uid = Uid::make()->encodeFromId(User::class, 1);

// something like: usr_86Rf07xd4z

use ShabuShabu\Uid\Facades\Uid;

// using the facade...
$uid = Uid::encode(User::find(1));

// something like: usr_86Rf07xd4z

// using the global function
$decoded = uid()->decode('usr_86Rf07xd4z');

// returns an instance of DecodedUid::class

use ShabuShabu\Uid\Service\Uid;

$model = Uid::make()->decodeToModel('usr_86Rf07xd4z');

// returns an instance of User::class

$model = Uid::make()->withTrashed()->decodeToModel('usr_86Rf07xd4z');

// returns an instance of User::class, even if the model is trashed

use ShabuShabu\Uid\Service\Uid;

$valid = Uid::make()->isValid('usr_86Rf07xd4z');

// returns true if the prefix exists

$valid = Uid::make()->isValid('usr_86Rf07xd4z', User::class);

// returns true if the prefix exists and belongs to the class

use ShabuShabu\Uid\Facades\Uid;

$alias = Uid::alias(User::class);

// returns usr

return [
    // ...
    'morph_map' => [
        'enabled' => true,
        'type' => 'regular',
    ],
];
bash
php artisan vendor:publish --tag="uid-config"
bash
php artisan uid:alphabet
bash
php artisan uid:info