1. Go to this page and download the library: Download malico/laravel-nanoid 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/ */
malico / laravel-nanoid example snippets
class YourModel Extends \Illuminate\Database\Eloquent\Model
{
/** @var array|int */
protected $nanoidLength = 10;
// id will be of length 10
// specifying to array. e.g [10, 20] will generate id of length 10 to 20
// or
public function nanoidLength(): array|int
{
// [10,20]
return 10;
}
/** @var string */
protected $nanoidPrefix = 'pl_'; // id will look: pl_2k1MzOO2shfwow ...
// or
public function nanoidPrefix(): string
{
return 'pay_'; // pay_2MII83829sl2d
}
/** @var string */
protected $nanoidAlphabet = 'ABC';
// or
public function nanoidAlphabet(): string {
return 'ABC'; // pay_ACBACB
}
public function uniqueIds()
{
// will create nanonids for 'unique_id' &'another_with'
// Also, won't break if id is not listed.
return ['unique_id', 'another_id'];
}
}
diff
// migration file
public function up()
{
Schema::create('test_models', function (Blueprint $table) {
- $table->id();
+ $table->string('id')->primary();
//
$table->timestamps();
});
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.