PHP code example of attla / ulid

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

    

attla / ulid example snippets


use Attla\Ulid\Factory as UlidFactory;

$ulid = UlidFactory::generate();
echo $ulid; // 01B8KYR6G8BC61CE8R6K2T16HY
echo $ulid->generate(); // 01B8KYR6G8BC61CE8R6K2T16HZ

// Or if you prefer a lowercased output
$ulid = UlidFactory::generate(true);
echo $ulid->get(); // 01b8kyr6g8bc61ce8r6k2t16hy

// If you need the timestamp from an ULID instance
$ulid = UlidFactory::generate();
echo $ulid->toTimestamp(); // 1561622862

// You can also generate a ULID for a specific UNIX-time in milliseconds
$ulid = UlidFactory::fromTimestamp(1593048767015);
// or with a lower cased output: $ulid = UlidFactory::fromTimestamp(1593048767015, true);
echo $ulid->toString(); // 01EBMHP6H7TT1Q4B7CA018K5MQ

$table->ulid();

Schema::create('items', function (Blueprint $table) {
  $table->ulid();
  ....
  ....
  $table->timestamps();
});

Schema::create('categories', function (Blueprint $table) {
  $table->ulid($ulidLength);
  ....
  // related model that uses ULID
  $table->foreignUlid($column, $foreignColumn, $foreignTable, $ulidLength);
  ....
  $table->timestamps();
});

use Illuminate\Database\Eloquent\Model;
use Attla\Ulid\HasUlid;

class Item extends Model
{
  use HasUlid;
}

// 'HasUlid' trait will automatically generate and assign id field.
$item = Item::create(['name' => 'Awesome item']);
echo $item->id;
// 01brh9q9amqp7mt7xqqb6b5k58
 php
Schema::create('items', function (Blueprint $table) {
  $table->ulid();
  ....
  // related model that uses ULID
  $table->foreignUlid('category_id');
  ....
  $table->timestamps();
});