Laravel 迁移增加表备注扩展方法

扩展 laravel 迁移 设置表备注

声明: 由于 第一次发布md文档, 如文档中不足之处请多多包涵和指教. 并且目前只实现了mysql的表注释


修改的完整代码

效果

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        SchemaExtends::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');

            //扩展的方法
            $table->tableComment('用户表');
        });
    }
  1. 重写迁移的门面(Schema)

如果需要扩展其他数据支持的话, 去掉 MySqlConnection 或者 改成其他接口

class SchemaExtends extends Facade
{
    /**
     * Get a schema builder instance for a connection.
     *
     * @param  string $name
     * @return \Illuminate\Database\Schema\Builder
     */
    public static function connection($name)
    {
        return static::getSchemaBuilder($name);
    }

    /**
     * Get a schema builder instance for the default connection.
     *
     * @return \Illuminate\Database\Schema\Builder
     */
    protected static function getFacadeAccessor()
    {
        return static::getSchemaBuilder();
    }

    /**
     * Get a schema builder instance for the default connection.
     *
     * @param string|null $name
     * @return \Illuminate\Database\Schema\Builder
     */
    protected static function getSchemaBuilder(string $name = null): Builder
    {
         //获取连接
        $connection = static::$app['db']->connection($name);

        //由于目前只实现mysql. 所以增加了限制
        if ($connection instanceof MySqlConnection) {
            //继承并且增加新的方法
            $grammar = $connection->withTablePrefix(new MySqlGrammar());
            $connection->setSchemaGrammar($grammar);
        }

        //获取构建器
        return $connection->getSchemaBuilder();
    }
}
  1. Blueprint 使用混入 (Macroable) 来新增对外的方法

这里是如何扩展的后续再说明

use Closure;

class Blueprint
{
    /**
     * 添加设置表备注
     *
     * @return \Closure
     */
    public function tableComment(): Closure
    {
        return function (string $comment) {
            //添加新的命令
            $this->addCommand('tableComment', compact('comment'));
        };
    }
}

注册服务

/**
 * Class MacroableServiceProvider
 *
 * @package LittleSuperman\Database\Providers
 */
class MacroableServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     * @throws \ReflectionException
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function boot(): void
    {
        //使用混入
        BlueprintSupport::mixin($this->app->make(Blueprint::class));
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
  1. 扩展迁移语法(MySqlGrammar)
class MySqlGrammar extends SuperMySqlGrammar
{
    /**
     * 设置表备注
     *
     * @param Blueprint $blueprint
     * @param Fluent    $command
     * @return string
     */
    public function compileTableComment(Blueprint $blueprint, Fluent $command): string
    {
        return "alter table {$this->wrapTable($blueprint)} comment ' {$command->comment}'";
    }
}

其他

相关后续说明 等有空在慢写

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

学习一波,大佬带我

4年前 评论

奇怪为什么官方的包就不能写个表注释的方法

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!