奥丁9
奥丁9
后端
数据库
redis
mysql
mongoDB
达梦
php
laravel
laravel-admin
dcat
表单
表格
java
spring
python
go
c
c++
前端
vue
nodejs
sass/less
html/css
前端框架
javascript
微信生态
公众号
小程序
uniapp
typescript
其他
AI
数据结构
安全
linux
seo
git
健身
算法
正则表达式
docker
待分类
后端
/
php
/
laravel
流式编写migration迁移文件
1年前
aoding9
89
php
laravel
没什么用,就是看着比较简洁,减少重复代码。 对Migration封装,设置默认值,然后返回$this,顺便把一些常用的字段再次封装,比如seo的tdk三个字段,或者order排序字段等    字段默认是允许为空,如果默认不为空的话,要调整一下 ``` <?php namespace Yang\Database; // 改成你自己的 use \Illuminate\Database\Migrations\Migration as BaseMigration; use Illuminate\Database\Schema\Blueprint; /** * @property Blueprint|null $blueprint * @property Blueprint|null $table 等同$blueprint */ class Migration extends BaseMigration { public $blueprint = null; public function onlyTable(Blueprint $table) { $this->blueprint = $table; return $this; } public function dropColumn($columns) { $this->blueprint->dropColumn($columns); return $this; } public function table(Blueprint $table, $id = true, $timestamps = true, $softDelete = true) { $this->blueprint = $table; $this->id($id)->timestamps($timestamps)->softDelete($softDelete); return $this; } public function bool($column = '', $comment = '', $default = 1) { $this->table->boolean($column)->default($default)->comment($comment); return $this; } public function active() { $this->bool('active', '是否启用'); return $this; } public function reason($column = 'reason', $comment = '原因') { $this->string($column, $comment); return $this; } public function status($default = 1) { $this->tinyInt('status', '状态', false, $default); return $this; } public function hidden() { $this->bool('hidden', '是否隐藏', 0); return $this; } public function show() { $this->bool('show', '是否显示', 1); return $this; } public function type($default = 1) { $this->tinyInt('type', '类型', true, $default); return $this; } public function state($default = 1) { $this->tinyInt('state', '状态', false, $default); return $this; } public function id($bool = true) { $bool && $this->table->id(); return $this; } public function softDelete($bool = true) { $bool && $this->table->softDeletes(); return $this; } public function timestamps($bool = true) { $bool && $this->table->timestamps(); return $this; } public function order() { $this->table->integer('order')->index()->default(0)->comment('排序'); return $this; } public function tree($titleColumn = 'title', $titleComment = '标题', $parentColumn = 'parent_id') { if ($titleColumn) { $this->title($titleColumn, $titleComment); } $this->foreignKey($parentColumn, '父级', false, 0) ->order() ->tinyInt('depth', '所处层级', false, 1); return $this; } public function title($column = 'title', $comment = '标题') { $this->string($column, $comment); return $this; } public function description($column = 'description', $comment = '描述') { $this->string($column, $comment); return $this; } public function keywords($column = 'keywords', $comment = '关键词') { $this->string($column, $comment); return $this; } public function tdk($titleColumn = 'title', $titleComment = '标题') { if ($titleComment) { $this->title($titleColumn, $titleComment); } $this->description()->keywords(); return $this; } public function comment($table, $comment) { \DB::statement("ALTER TABLE `$table` comment '$comment'"); return $this; } public function json($column, $comment = '', $nullable = true, $default = null) { $this->table->json($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function time($column, $comment = '', $nullable = true, $default = null) { $this->table->time($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function timestamp($column, $comment = '', $nullable = true, $default = null) { $this->table->timestamp($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function datetime($column, $comment = '', $nullable = true, $default = null) { $this->table->datetime($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function date($column, $comment = '', $nullable = true, $default = null) { $this->table->date($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function string($column, $comment = '', $nullable = true, $default = null) { $this->table->string($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function morphs($name, $indexName = null) { $this->table ->morphs($name, $indexName); return $this; } public function smallInt($column, $comment = '', $nullable = true, $default = null) { $this->table->smallInteger($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function tinyInt($column, $comment = '', $nullable = true, $default = null) { $this->table->tinyInteger($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function float($column, $comment = '', $nullable = true, $default = 0) { $this->table->float($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function int($column, $comment = '', $nullable = true, $default = 0) { $this->table->integer($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function bigInt($column, $comment = '', $nullable = true, $default = 1) { $this->table->bigInteger($column)->nullable($nullable)->default($default)->comment($comment); return $this; } public function foreignKey($column, $comment = '', $nullable = true, $default = null) { $this->table->unsignedBigInteger($column)->nullable($nullable)->default($default)->comment($comment)->index(); return $this; } public function text($column, $comment = '', $nullable = true, $default = null, $isChange = false) { $column = $this->table->text($column)->nullable($nullable)->default($default)->comment($comment); if ($isChange) { $column->change(); } return $this; } public function longText($column, $comment = '', $nullable = true, $default = null, $isChange = false) { $column = $this->table->longText($column)->nullable($nullable)->default($default)->comment($comment); if ($isChange) { $column->change(); } return $this; } public function content($column = 'content', $comment = '内容', $type = 'text') { switch ($type) { case 'text': $this->text($column, $comment); break; case 'string': $this->string($column, $comment); break; case 'longtext': $this->longText($column, $comment); break; } return $this; } public function name($column = 'name', $comment = '名称') { $this->string($column, $comment); return $this; } public function link($column = 'link', $comment = '链接') { $this->string($column, $comment); return $this; } public function value($column = 'value', $comment = '值') { $this->string($column, $comment); return $this; } public function username($column = 'username', $comment = '用户名') { $this->string($column, $comment); return $this; } public function password($column = 'password', $comment = '密码') { $this->string($column, $comment); return $this; } public function phone($column = 'phone', $comment = '手机') { $this->string($column, $comment); return $this; } public function email($column = 'email', $comment = '邮箱') { $this->string($column, $comment); return $this; } public function rememberToken($column = 'remember_token', $comment = '记住我') { $this->string($column, $comment); return $this; } public function avatar($column = 'avatar', $comment = '头像') { $this->image($column, $comment); return $this; } /** * @Desc 简介 * @param string $column * @param string $comment * @return $this * @Date 2022/6/9 14:44 */ public function introduction($column = 'introduction', $comment = '简介') { $this->string($column, $comment); return $this; } public function number($column = 'number', $comment = '编号') { $this->string($column, $comment); return $this; } public function image($column = 'image', $comment = '图片', $isText = false) { if ($isText) { $this->text($column, $comment); } else { $this->string($column, $comment); } return $this; } public function litpic($column = 'litpic', $comment = '缩略图') { $this->image($column, $comment); return $this; } public function index($columns, $name = null, $algorithm = null) { $this->table->index($columns, $name, $algorithm); return $this; } public function unique($columns, $name = null, $algorithm = null) { $this->table->unique($columns, $name, $algorithm); return $this; } public function __get($name) { if ($name === 'table' && !isset($this->table)) { // dd($this->blueprint); if (!$this->blueprint) { throw new \Exception('$table未定义,请先调用table方法设置$table'); } return $this->blueprint; } } public function __set($name, $value) { } public function __isset($name) { } public function arctypeId($column = 'arctype_id', $comment = '栏目') { $this->foreignKey($column, $comment); return $this; } public function articleId($column = 'article_id', $comment = '文章') { $this->foreignKey($column, $comment); return $this; } public function categoryId($column = 'category_id', $comment = '栏目') { $this->foreignKey($column, $comment); return $this; } public function adminUserId($column = 'admin_user_id', $comment = '管理员') { $this->foreignKey($column, $comment); return $this; } public function userId($column = 'user_id', $comment = '用户') { $this->foreignKey($column, $comment); return $this; } public function openid() { $this->string('openid')->unique('openid'); return $this; } public function wxSessionKey() { $this->string('wx_session_key'); return $this; } public function level() { $this->tinyInt('level', '等级'); return $this; } public function decimal($column = '', $comment = '管理员', $total = 18, $place = 6) { $this->table->decimal($column, $total, $place)->nullable()->comment($comment); return $this; } public function position() { $this->table->decimal('longitude', 10, 7)->nullable()->comment('经度'); $this->table->decimal('latitude', 10, 7)->nullable()->comment('纬度'); return $this; } public function views() { $this->int('views', '浏览量'); return $this; } public function shares() { $this->int('shares', '分享数'); return $this; } public function result($comment = '结果') { $this->string('result', $comment); return $this; } public function author() { $this->string('author', '作者'); return $this; } public function source() { $this->string('source', '来源'); return $this; } public function url($comment = 'url') { $this->string('url', $comment); return $this; } public function key() { $this->string('key', '键'); return $this; } } ```
本作品采用
《CC 协议》
,转载必须注明作者和本文链接