Jul
14
2020
Laravel生成数据表字典文档
在写文档的时候需要用到数据表字典数据,一个个建立文档比较麻烦,偷个懒写了个脚本,我这里文档使用的是ShowDoc,因为他连得是SQLite数据库,所以使用时注意添加对应的数据库配置。
Route::get('/resolveSql', function () { $res = DB::select('show tables'); // echo '<pre>'; foreach ($res as $v) { $tableName = $v->Tables_in_loan; $sql = <<<EOF SELECT -- t.TABLE_SCHEMA AS 'database', -- t.TABLE_NAME AS 'table_name', t.COLUMN_NAME AS 'field', t.COLUMN_TYPE AS 'data_type', CASE IFNULL(t.COLUMN_DEFAULT,'Null') WHEN '' THEN '空字符串' WHEN 'Null' THEN 'NULL' ELSE t.COLUMN_DEFAULT END AS 'default', CASE t.IS_NULLABLE WHEN 'YES' THEN '是' ELSE '否' END AS 'is_null', t.COLUMN_COMMENT AS 'comment' FROM information_schema.COLUMNS t WHERE t.TABLE_SCHEMA='loan' AND t.TABLE_NAME='{$tableName}'; EOF; $table = DB::select($sql); $tableDoc = ''; foreach ($table as $field) { $tableDoc .= "|{$field->field}|{$field->data_type}|{$field->is_null}|{$field->default}|{$field->comment}|\n"; } $doc = <<<EOF - {$tableName} |字段|类型|是否为空|默认值|注释| |---|---|---|---|---| {$tableDoc} > 备注: EOF; DB::connection('sqlite2')->table('page')->insert([ 'author_uid' => 2, 'author_username' => 'Veris', 'item_id' => 4, 'cat_id' => 11, 'page_title' => $tableName, 'page_content' => $doc, 's_number' => 99, 'addtime' => time(), 'page_comments' => '', 'is_del' => 0, ]); echo $tableName . '插入成功<br>'; // die; } return 'SUCCESS'; });
=================================================
转载请注明出处:
作者:Veris
最族 [ http://www.mostclan.com ]
发表评论: