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;

class User extends Model
{
    use HasUid;
}

use ShabuShabu\Uid\Service\Uid;

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

// something like: usr_86Rf07xd4z

use ShabuShabu\Uid\Service\Uid;

$uid = Uid::make()->encode(User::find(1));

// something like: usr_86Rf07xd4z

use ShabuShabu\Uid\Service\Uid;

$decoded = Uid::make()->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\Service\Uid;

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

// returns usr

use Illuminate\Database\Eloquent\Relations\Relation;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Relation::enforceMorphMap(config('uid.prefixes'));
    }
}
bash
php artisan vendor:publish --tag="uid-config"
bash
php artisan uid:alphabet
bash
php artisan uid:info