PHP code example of cbenjafield / laravel-nano-ids

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

    

cbenjafield / laravel-nano-ids example snippets


use Cbenjafield\LaravelNanoIds\HasNanoId;

// Optional: specify a custom length for the Nano ID
protected function getNanoIdSize(): int
{
    return 16; // Default is 21
}

use Illuminate\Database\Eloquent\Model;
use Cbenjafield\LaravelNanoIds\HasNanoId;

class MyModel extends Model
{
    use HasNanoId;

    protected $primaryKey = 'custom_id'; // Use 'custom_id' instead of 'id'
}

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

public function up()
{
    Schema::create('my_models', function (Blueprint $table) {
        $table->primaryNanoId('custom_id'); // Creates a primary key with Nano ID
        $table->nanoId('another_nano_id'); // Creates a regular Nano ID column
        $table->nanoId('nano_id_with_length', 16); // Creates a Nano ID with a custom length
        
        $table->string('name');
        $table->timestamps();
    });
}
bash
php artisan vendor:publish --provider="Cbenjafield\LaravelNanoIds\NanoIdServiceProvider"