Skip to content

Commit

Permalink
1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
yondifon committed Aug 8, 2023
1 parent 3769659 commit c24f861
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 318 deletions.
31 changes: 31 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Upgrade Guide

## 0.x to 1.x

Previously we had model within that package you had to extend but now you don't, you just need to use our trait like you would with `HasUlid` & `HasUuid` in Laravel

```diff
<?php

namespace App\Models;

- use Malico\LaravelNanoid\Eloquent\Model;
+ use Illuminate\Database\Eloquent\Model;
+ use Malico\LaravelNanoid\HasNanoids;

class Payment extends Model {
+ use HasNanoids;
}
```

Also, we added, `nanoidPrefix` and `nanoidLength`, like

```php
public function nanoidLength(): int {
return 2;
}

public function nanoidPrefix(): string {
return 'pay_'
}
```
104 changes: 20 additions & 84 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,31 @@ A simple drop-in solution for providing nanoid support for the IDs of your Eloqu

## Usage

There are two ways to use this package:
Use nanoid within your model, use the trait `HasNanoids` within your model like.

- By extending the provided model classes (preferred and simplest method).
- By using the provided model trait (allows for extending another model class).

### Extend the model classes

While creating your model, you can extend the `Eloquent Model` class provided by the package.

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Malico\LaravelNanoid\Eloquent\Model;

class Book extends Model
{
use HasFactory;
}
```

To create model fast and easy, use the artisan command `make:nanoid-model`, same as you will do with the `make:model` command.
All arguments work the same as the `make:model` command. To create migration file with string id (which is what is needed), add the `-m` option, the migration file created will have id of string type (string) instead of autoincrementing integer type.

### Extend the model trait

With the `ModelTrait` trait, all you need to do is add the trait to your model class, then make you sure model properties look like this:

```php
```diff
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
namespace Malico\LaravelNanoid\Eloquent\InteractsWithNanoid;
+ use Malico\LaravelNanoid\HasNanoids;

class Book extends Model{
use HasFactory;
use InteractsWithNanoid;

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* The "booting" method of the model.
*/
protected static function boot(): void
{
parent::boot();

static::creating(function (self $model): void {
$model->{$model->getKeyName()} = $model->generateNanoid();
});
}
+ use HasNanoids;
}
```

Take note of the `$incrementing` and `$keyType` properties. Also make sure within your `boot` method you call the `parent::boot` method and then add the `creating` event listener.
Also make sure your id column is set to `string` type.
Your migration file should like.

```php
```diff
// migration file
public function up()
{
Schema::create('test_models', function (Blueprint $table) {
$table->string('id')->primary();
- $table->id();
+ $table->string('id')->primary();
//
$table->timestamps();
});
Expand All @@ -113,35 +52,32 @@ To create a new migration, use the artisan command `make:nanoid-migration`. All
<?php

/**
* Nano id length.
*
* @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

public function nanoidLength(): array|int
{
// [10,20]
return 10;
}

/**
* Nano id prefix.
*
* @var string
*/
protected $nanoidPrefix = 'pl_'; // id will look: pl_2k1MzOO2shfwow ...

```

This is inspired by the [Laravel Eloquent UUID](https://github.com/goldspecdigital/laravel-eloquent-uuid) package and stripe's transaction ids.

If you want to customize the user model, Replace the follows namespace in your user model.

```php

// use Illuminate\Foundation\Auth\User as Authenticatable;
use Malico\LaravelNanoid\Auth\User as Authenticatable;
public function nanoidPrefix(): string
{
return 'pay_'; // pay_2MII83829sl2d
}

#class User extends Authenticatable
```

Check the upgrade guide if you're [upgrading](UPGRADE.MD) from 0.x

### Author

Ndifon Desmond Yong
58 changes: 0 additions & 58 deletions src/Auth/User.php

This file was deleted.

33 changes: 2 additions & 31 deletions src/Console/Commands/NanoidModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,13 @@ class NanoidModelMakeCommand extends ModelMakeCommand
*/
protected function getStub(): string
{
return realpath(__DIR__.'/../../Eloquent/stubs/nanoid.model.stub');
}

/**
* Get the console command options.
*/
protected function getOptions(): array
{
// Remove the pivot option from the parent class.
return array_filter(
parent::getOptions(),
fn (array $option): bool => $option[0] !== 'pivot'
);
return realpath(__DIR__.'/stubs/nanoid.model.stub');
}

/**
* Create a migration file for the model.
*
* @return void
*/
protected function createMigration()
protected function createMigration(): void
{
$table = Str::snake(Str::pluralStudly(class_basename($this->argument('name'))));

Expand All @@ -62,19 +48,4 @@ protected function createMigration()
'--create' => $table,
]);
}

/**
* Get the value of a command option.
*
* @param null|string $key
* @return null|array|bool|string
*/
public function option($key = null)
{
if ($key === 'pivot') {
return false;
}

return parent::option($key);
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace {{ namespace }};

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Malico\LaravelNanoid\Eloquent\Model;
use Illuminate\Database\Eloquent\Model;
use Malico\LaravelNanoid\HasNanoids;

class {{ class }} extends Model
{
use HasFactory;
use HasNanoids;
}
2 changes: 1 addition & 1 deletion src/Database/Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public function __construct(Filesystem $files, $customStubPath = null)

public function stubPath()
{
return __DIR__.'/stubs';
return __DIR__.'/../../Console/Commands/stubs';
}
}
30 changes: 0 additions & 30 deletions src/Eloquent/InteractsWithNanoid.php

This file was deleted.

57 changes: 0 additions & 57 deletions src/Eloquent/Model.php

This file was deleted.

Loading

0 comments on commit c24f861

Please sign in to comment.