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 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。