├── composer.json ├── LICENSE ├── generator.php └── README.md /composer.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "name": "saade-package/generate-unique-id", 4 | "type": "library", 5 | "description": "A package to generate unique ID", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Moemen saade", 10 | "email": "moemensaadeh936@gmail.com", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=7.0" 16 | }, 17 | "autoload": { 18 | "files": [ 19 | "generator.php" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 moemen saadeh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /generator.php: -------------------------------------------------------------------------------- 1 | where($column, $id)->count(); 24 | 25 | 26 | // Check if the ID already exists in the database (native php) 27 | // $count = SELECT COUNT(*) FROM table WHERE column = id; 28 | if ($count == 0) { 29 | $unique = true; 30 | } 31 | } 32 | return $id; 33 | } 34 | 35 | 36 | ?> 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ID-GENREATOR 😃 2 | 3 | it generates an private id for database 4 | 5 | This code is using in laravel application to generate an unique id and also it will check if the generated id
6 | 7 | is the same in database it will generate a new . 😉 8 | 9 |

10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | # Installing : 24 | 25 | 26 | ``` 27 | composer require saade-package/generate-unique-id 28 | ``` 29 | 30 | 31 | 32 |

33 | 34 | 35 | 36 |

Now how to use it ?

37 | 38 | For example if you created an id-generator column in post table, you can make an private id to the post like this : 39 | 40 | 41 | 42 | ```php 43 | public function store(Request $request){ 44 | 45 | DB::table('generated_id')->insert([ 46 | 'generatedID'=>generateUniqueId('generatedIdTable','generatedIdColumn') 47 | ]); 48 | 49 | } 50 | ``` 51 | 52 | | Option | Description | 53 | | ------ | ----------- | 54 | | table name| (string & require) it is the table name where You want to insert the data in it. | 55 | | column name| (string & require) it is the table column where You want to insert the data in it. | 56 | 57 | 58 | 59 | The generate function generateUniqueId function takes 2 arguments , the first argument is the Table name and
60 | 61 | the second argument is the column name where you want to insert the uniqueId . 62 | 63 | Here this code will generate an id from 25 characters and inserts it in database . 64 | 65 | 66 | --------------------------------------------------------------------------------