PHP code example of vanthao03596 / laravel-ulid

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

    

vanthao03596 / laravel-ulid example snippets


// Default ulid generator with the current timestamp & lowercase string
Str::ulid(); // 01FDRXQ57VR4K4RASPT9NPAQC0
// Default ulid generator with the current timestamp & uppercase string
Str::ulid(false); // 01fdrxpmg9njp20z3km461fgax
// Default ulid generator with the given datetime & uppercase string
Str::ulid(false, Carbon::now()->subHour()) // 01FDRTD2K8Z664S24X606N14KD

Schema::create('foos', function (Blueprint $table) {
    $table->ulid('id')->primary(); // adds primary ulid column 
    $table->foreignUlid('user_id')->constrained()->cascadeOnDelete(); // adds ulid foreignkey
    $table->ulidMorphs('taggable'); // adds ulid morphs
    $table->nullableUlidMorphs('likeable'); // adds nullable ulid morphs
});



namespace App;

use Illuminate\Database\Eloquent\Model;
use Vanthao03596\LaravelUlid\GeneratesUlid;

class User extends Model
{
    use GeneratesUlid;
}


class User extends Model
{
    public function ulidColumn(): string
    {
        return 'custom_column';
    }
}

class User extends Model
{
    public function ulidColumns(): array
    {
        return ['ulid', 'custom_column'];
    }
}