PHP code example of parables / laravel-model-nanoid
1. Go to this page and download the library: Download parables/laravel-model-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/ */
parables / laravel-model-nanoid example snippets
namespace App;
use Illuminate\Database\Eloquent\Model;
use Parables\NanoId\GeneratesNanoId;
class Post extends Model
{
use GeneratesNanoId;
}
class Post extends Model
{
public static function nanoIdColumn(): string
{
return 'id';
}
}
namespace App;
use Parables\NanoId\NanoIdAsPrimaryKey;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use NanoIdAsPrimaryKey;
}
// Find a specific post with the default (nanoId) column name
$post = Post::whereNanoId($nanoId)->first();
// Find multiple posts with the default (nanoId) column name
$post = Post::whereNanoId([$first, $second])->get();
// Find a specific post with a custom column name
$post = Post::whereNanoId($nanoId, 'custom_column')->first();
// Find multiple posts with a custom column name
$post = Post::whereNanoId([$first, $second], 'custom_column')->get();
public function getRouteKeyName(): string
{
return 'nanoId';
}
class Post extends Model
{
public static function nanoIdColumns(): array
{
return ['nanoId', 'custom_column'];
}
}
public static function nanoIdColumns(): array
{
// Option 1: array of column names: this will use the default size and alphabets
return ['nanoId', 'custom_column'];
// Option 2: an array where each element is an array with a 6, 'alphabets'=> NanoId::ALPHABET_NUMBERS],
['key'=>'column_two', 'size' => 10, 'alphabets'=> NanoId::ALPHABET_UUID],
];
// Option 3: an array with a string key and an array value with an optional 'size' and 'alphabet' property. If a 'key' is passed in the value, it overwrites the original array 'key'.
return [
['key'=>'nanoId'], // use the NanoId::SIZE_DEFAULT = 21; and NanoId::ALPHABET_DEFAULT
'column_one' => ['size' => 6, 'alphabets'=> NanoId::ALPHABET_NUMBERS],
// will use 'another_column' as the column name instead of 'column_two'
'column_two' => ['key'=>'another_column', 'size' => 10, 'alphabets'=> NanoId::ALPHABET_UUID],
];
}
NanoId::ALPHABET_DEFAULT => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
NanoId::ALPHABET_NUMBERS =>'0123456789'NanoId::ALPHABET_NUMBERS_READABLE => '346789'
NanoId::ALPHABET_LOWERCASE => 'abcdefghijklmnopqrstuvwxyz'
NanoId::ALPHABET_LOWERCASE_READABLE => 'abcdefghijkmnpqrtwxyz'
NanoId::ALPHABET_UPPERCASE => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
NanoId::ALPHABET_UPPERCASE_READABLE => 'ABCDEFGHIJKMNPQRTWXYZ'
NanoId::ALPHABET_ALPHA_NUMERIC => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
// Numbers and English alphabet without unreadable letters: 1, l, I, 0, O, o, u, v, 5, S, s, 2, Z
NanoId::ALPHABET_ALPHA_NUMERIC_READABLE => '346789abcdefghijkmnpqrtwxyzABCDEFGHJKLMNPQRTUVWXY'
// Same as ALPHABET_ALPHA_NUMERIC_READABLE but with removed vowels and following letters: 3, 4, x, X, V.
NanoId::ALPHABET_ALPHA_NUMERIC_READABLE_SAFE => '6789bcdfghjkmnpqrtwzBCDFGHJKLMNPQRTW'
NanoId::ALPHABET_UUID => '0123456789abcdef'
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.