Skip to content

Commit 165c120

Browse files
committed
add ability to generate auto increment ID and customized ID
1 parent 5c2808d commit 165c120

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
],
2828
"require": {
2929
"php": "^7.4|^8.0",
30+
"haruncpi/laravel-id-generator": "^1.0",
3031
"illuminate/console": "^8.0",
3132
"illuminate/container": "^8.0",
3233
"illuminate/contracts": "^8.0",

src/Repository/Concerns/CreatesEntity.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Bssd\EloquentRepository\Repository\Concerns;
44

5+
use Haruncpi\LaravelIdGenerator\IdGenerator;
56
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Support\Facades\DB;
79

810
/**
911
* @property-read Builder|Model $model
@@ -22,4 +24,35 @@ public function create($properties)
2224
{
2325
return $this->model->create($properties);
2426
}
27+
28+
/**
29+
* Get next auto increment ID
30+
*
31+
* @return int
32+
*/
33+
public function getAutoIncrementedId(): int
34+
{
35+
$inspector = DB::select("SHOW TABLE STATUS LIKE '".$this->model->getTable()."'");
36+
return $inspector[0]->Auto_increment;
37+
}
38+
39+
/**
40+
* Get next customized ID
41+
*
42+
* @param string $field
43+
* @param int $length
44+
* @param string $prefix
45+
*
46+
* @return string
47+
* @throws \Exception
48+
*/
49+
public function getCustomizedId(string $field, int $length, string $prefix): string
50+
{
51+
return IdGenerator::generate([
52+
'table' => $this->model->getTable(),
53+
'field' => $field,
54+
'length' => $length,
55+
'prefix' => $prefix
56+
]);
57+
}
2558
}

0 commit comments

Comments
 (0)