├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── src ├── MySqlGrammar.php └── Schema.php └── tests └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | /.idea 3 | /composer.lock 4 | /vendor 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | laravel-schema-extend 2 | ===================== 3 | 4 | [![License](http://www.wtfpl.net/wp-content/uploads/2012/12/wtfpl-badge-1.png)](LICENSE) 5 | 6 | - support MySQL 'table comment'. 7 | - 'column comment' is built-in in greater than 5.1 version. 8 | - support variable length for integer/tinyint/mediumint/smallint/bigint 9 | 10 | --- 11 | 12 | > just extend the original class 13 | 14 | 15 | ## Install 16 | 17 | Require this package with composer using the following command: 18 | 19 | ```bash 20 | composer require zedisdog/laravel-schema-extend 21 | ``` 22 | 23 | ### less than 5.5 24 | modify the alias `Schema` in `config/app.php`: 25 | 26 | ```php 27 | 'aliases' => [ 28 | ... 29 | // 'Schema' => Illuminate\Support\Facades\Schema::class, 30 | 'Schema' => Jialeo\LaravelSchemaExtend\Schema::class, 31 | ], 32 | ``` 33 | 34 | ### great than 5.5 35 | just modify `use` statement from 36 | ```php 37 | use Illuminate\Support\Facades\Schema; 38 | ``` 39 | to 40 | ```php 41 | use Jialeo\LaravelSchemaExtend\Schema; 42 | ``` 43 | in migrate files. 44 | 45 | ## Usage 46 | 47 | ```php 48 | Schema::create('tests', function ($table) { 49 | //this is alredy built-in. 50 | $table->increments('id')->comment('column comment'); 51 | 52 | $table->integer('int')->default(1)->length(1); 53 | $table->bigInteger('big')->default(1)->length(1); 54 | $table->smallInteger('small')->default(1)->length(1); 55 | $table->tinyInteger('tiny')->default(1)->length(1); 56 | $table->mediumInteger('medium')->default(1)->length(1); 57 | 58 | $table->comment = 'table comment'; 59 | $table->autoIncrement = 100; 60 | }); 61 | ``` 62 | 63 | ## Thanks 64 | 65 | - [jialeo](https://github.com/jialeo) 66 | - [ghostboyzone](https://github.com/ghostboyzone) 67 | - [xuhuan](https://github.com/xuhuan) 68 | - [xiaobeicn](https://github.com/xiaobeicn) 69 | - [5-say](https://github.com/5-say) 70 | 71 | 72 | ## PS. 73 | sorry for my bad english -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "zedisdog/laravel-schema-extend", 3 | "type" : "library", 4 | "license" : "WTFPL", 5 | "description" : "supplement for eloquent migration", 6 | "authors" : [ 7 | { 8 | "name": "zed", 9 | "email": "2692273254@qq.com" 10 | } 11 | ], 12 | "require" : { 13 | "illuminate/support": ">=5.4", 14 | "illuminate/database": ">=5.4", 15 | "php" : ">=5.4.0" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": "^5.0|^6.0|^8.4|^9.0" 19 | }, 20 | "autoload" : { 21 | "psr-4" : { 22 | "Jialeo\\LaravelSchemaExtend\\" : "src/" 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/MySqlGrammar.php: -------------------------------------------------------------------------------- 1 | comment)) 25 | { 26 | $comment = str_replace("'", "\'", $blueprint->comment); 27 | if (is_array($sql)) { 28 | $sql[0] .= " comment = '".$comment."'"; 29 | } else { 30 | $sql .= " comment = '".$comment."'"; 31 | } 32 | } 33 | // 添加自增起始值 34 | if (isset($blueprint->autoIncrement)) { 35 | $blueprint->autoIncrement = intval($blueprint->autoIncrement); 36 | $sql .= " auto_increment = {$blueprint->autoIncrement}"; 37 | } 38 | return $sql; 39 | } 40 | 41 | 42 | /** 43 | * 重载typeInteger 44 | * Create the column definition for an integer type. 45 | * 46 | * @param \Illuminate\Support\Fluent $column 47 | * @return string 48 | */ 49 | public function typeInteger(Fluent $column) 50 | { 51 | $length_str = !empty($column->length) ? '(' . $column->length . ')' : ''; 52 | 53 | return 'int' . $length_str; 54 | } 55 | 56 | /** 57 | * 重载typeBigInteger 58 | * Create the column definition for a big integer type. 59 | * 60 | * @param \Illuminate\Support\Fluent $column 61 | * @return string 62 | */ 63 | protected function typeBigInteger(Fluent $column) 64 | { 65 | $length_str = !empty($column->length) ? '(' . $column->length . ')' : ''; 66 | 67 | return 'bigint' . $length_str; 68 | } 69 | 70 | /** 71 | * 重载typeMediumInteger 72 | * Create the column definition for a medium integer type. 73 | * 74 | * @param \Illuminate\Support\Fluent $column 75 | * @return string 76 | */ 77 | protected function typeMediumInteger(Fluent $column) 78 | { 79 | $length_str = !empty($column->length) ? '(' . $column->length . ')' : ''; 80 | 81 | return 'mediumint' . $length_str; 82 | } 83 | 84 | /** 85 | * 重载typeTinyInteger 86 | * Create the column definition for a tiny integer type. 87 | * 88 | * @param \Illuminate\Support\Fluent $column 89 | * @return string 90 | */ 91 | protected function typeTinyInteger(Fluent $column) 92 | { 93 | $length_str = !empty($column->length) ? '(' . $column->length . ')' : ''; 94 | 95 | return 'tinyint' . $length_str; 96 | } 97 | 98 | /** 99 | * 重载typeSmallInteger 100 | * Create the column definition for a small integer type. 101 | * 102 | * @param \Illuminate\Support\Fluent $column 103 | * @return string 104 | */ 105 | protected function typeSmallInteger(Fluent $column) 106 | { 107 | $length_str = !empty($column->length) ? '(' . $column->length . ')' : ''; 108 | 109 | return 'smallint' . $length_str; 110 | } 111 | } -------------------------------------------------------------------------------- /src/Schema.php: -------------------------------------------------------------------------------- 1 | connection($name); 22 | return static::useCustomGrammar($connection); 23 | } 24 | 25 | /** 26 | * Get a schema builder instance for the default connection. 27 | * 28 | * @return \Illuminate\Database\Schema\Builder 29 | */ 30 | protected static function getFacadeAccessor() 31 | { 32 | $connection = static::$app['db']->connection(); 33 | return static::useCustomGrammar($connection); 34 | } 35 | 36 | 37 | /** 38 | * lead the system to load custom Grammar 39 | * @param $connection 40 | */ 41 | protected static function useCustomGrammar($connection) 42 | { 43 | // just for MySqlGrammar 44 | if (get_class($connection) === 'Illuminate\Database\MySqlConnection') { 45 | $MySqlGrammar = $connection->withTablePrefix(new MySqlGrammar); 46 | $connection->setSchemaGrammar($MySqlGrammar); 47 | } 48 | 49 | return $connection->getSchemaBuilder(); 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zedisdog/laravel-schema-extend/5da28586258f4341667239d3648719a0dfcf8885/tests/.gitkeep --------------------------------------------------------------------------------