-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit adebee1
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Laravel ID Generator | ||
Easy way to generate custom ID from database table in Laravel framework | ||
|
||
# Documentation | ||
Get documentation on [laravelarticle.com](https://laravelarticle.com/laravel-custom-id-generator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "haruncpi/laravel-id-generator", | ||
"description": "Easy way to generate custom ID in laravel framework", | ||
"license": "cc-by-4.0", | ||
"authors": [ | ||
{ | ||
"name": "Md.Harun-Ur-Rashid", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"require": { | ||
"php": ">=5.6.0", | ||
"laravel/framework": "5.*" | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Haruncpi\\IdGenerator\\IdGeneratorServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Haruncpi\IdGenerator; | ||
|
||
use DB, Exception; | ||
|
||
class IdGenerator | ||
{ | ||
|
||
public static function generate($configArr) | ||
{ | ||
if (!array_key_exists('table', $configArr) || $configArr['table'] == '') { | ||
throw new Exception('Must need a table name'); | ||
} | ||
if (!array_key_exists('length', $configArr) || $configArr['length'] == '') { | ||
throw new Exception('Must specify the length of ID'); | ||
} | ||
if (!array_key_exists('prefix', $configArr) || $configArr['prefix'] == '') { | ||
throw new Exception('Must specify a prefix of your ID'); | ||
} | ||
|
||
if (array_key_exists('where', $configArr)) { | ||
if (is_string($configArr['where'])) | ||
throw new Exception('where clause must be an array, you provided string'); | ||
if (!count($configArr['where'])) | ||
throw new Exception('where clause must need at least an array'); | ||
} | ||
|
||
|
||
$prefixLength = strlen($configArr['prefix']); | ||
$idLength = $configArr['length'] - $prefixLength; | ||
$whereString = ''; | ||
|
||
if (array_key_exists('where', $configArr)) { | ||
$whereString .= " WHERE "; | ||
foreach ($configArr['where'] as $row) { | ||
$whereString .= $row[0] . "=" . $row[1] . " AND "; | ||
} | ||
} | ||
|
||
$whereString = rtrim($whereString, 'AND '); | ||
|
||
|
||
$total = DB::select("SELECT count(id) total FROM " . $configArr['table'] . $whereString . ""); | ||
|
||
if ($total[0]->total) { | ||
$maxId = DB::select("SELECT MAX(SUBSTR(id," . ($prefixLength + 1) . "," . $idLength . ")) maxId | ||
FROM " . $configArr['table'] . $whereString . ""); | ||
$maxId = $maxId[0]->maxId + 1; | ||
|
||
return $configArr['prefix'] . str_pad($maxId, $idLength, '0', STR_PAD_LEFT); | ||
} else { | ||
return $configArr['prefix'] . str_pad(1, $idLength, '0', STR_PAD_LEFT); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php namespace Haruncpi\IdGenerator; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class IdGeneratorServiceProvider extends ServiceProvider | ||
{ | ||
|
||
public function boot() | ||
{ | ||
|
||
} | ||
|
||
|
||
public function register() | ||
{ | ||
$this->app->make('Haruncpi\IdGenerator\IdGenerator'); | ||
} | ||
|
||
} |