PHP code example of waska14 / laravel-uuid

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

    

waska14 / laravel-uuid example snippets


'providers' => [
    ....
    Waska\LaravelUuid\UuidServiceProvider::class,
    ....
],

// 2.1 If you want to use uuid column as primary column:
Schema::create('students', function(Blueprint $table)
{
    $table->uuid('uuid')->primary();
    $table->string('name');
    $table->string('last_name');
    ...
});

// 2.2 If you want to use uuid column as non-primary column:
Schema::create('students', function(Blueprint $table)
{
    $table->increments('id');
    $table->uuid('uuid')->unique();
    $table->string('name');
    $table->string('last_name');
    ...
});

// 2.3 If you want to use multiple uuid columns (maybe on of them is primary):
Schema::create('students', function(Blueprint $table)
{
    $table->uuid('uuid')->primary();
    $table->uuid('uuid_column_2')->unique();
    $table->uuid('uuid_column_3')->unique();
    $table->string('name');
    $table->string('last_name');
    ...
});

class Student extends Model
{
    use \Waska\Traits\Uuid;
}

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $primaryKey = "uuid_primary_column_name";
    protected $uuid_column = "uuid_primary_column_name";

    protected $fillable = [
        'name',
        'last_name',
    ];
}

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $fillable = [
        'name',
        'last_name',
        'uuid', // config/waska.uuid.php -> default_column_name
    ];
}

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $uuid_column = "uuid_column_name";
    
    protected $fillable = [
        'name',
        'last_name',
        'uuid_column_name',
    ];
}

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $primaryKey = "uuid_primary_column_name"; // If one of them is primary
    protected $uuid_column = ["uuid_column_name1", "uuid_column_name2", "uuid_column_name3"];
    
    protected $fillable = [
        'name',
        'last_name',
        'uuid_column_name1',
        'uuid_column_name2',
        'uuid_column_name3',
    ];
}

Student::create([
    'name'      => "Bill',
    'last_name' => "Gates',
]);

// or
$student = new Student([
    'name'      => "Bill',
    'last_name' => "Gates',
]);
$student->save();

// or
$student = new Student();
$student->name = "Bill";
$student->last_name = "Gates";
$student->save();

Waska\Uuid::get(4); // This command will generate valid uuid (v4, pseudo-random) string

/**
 * This generates name-based Uuid
 * @param int $version.
 * @param string $name. String which the uuid is generating for.
 * @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
 * @return String
 */
Waska\Uuid::get(3, "some_random_string", "valid_uuid");

// This generates pseudo-random Uuid
Waska\Uuid::get(); // Default version is 4, so it means: Waska\Uuid::get(4);

/**
 * This generates name-based Uuid
 * @param int $version.
 * @param string $name. String which the uuid is generating for.
 * @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
 * @return String
 */
Waska\Uuid::get(5, "some_random_string", "valid_uuid");
bash
php artisan vendor:publish --tag=waska-uuid-config