Laravel 实现数据库和迁移文件的双向同步

2022-07-13  本文已影响0人  Yohann丶blog
WechatIMG1462.jpeg

介绍

使用 laravel 框架的小伙伴,这将会是你的福音!推荐使用 yuhal/laravel-sync-database,可以帮助您高效开发哦,欢迎 star OR fork!如果您不想在开发laravel项目时手动创建迁移文件。它可以帮助您自动创建和更新数据库中表结构对应的迁移文件。此外,它还可以与早期同步迁移结合使用,实现数据库和迁移文件的双向同步。

安装

$ composer create-project --prefer-dist laravel/laravel blog
$ cd blog
$ cp .env.example .env
# 项目地址
APP_URL=http://blog.com
# mysql数据库连接
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
$ composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
$ composer install
$ composer require kitloong/laravel-migrations-generator=5.0.1
$ composer require awssat/laravel-sync-migration
$ composer require yuhal/laravel-sync-database

同步

$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (19.90ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (15.12ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (16.40ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (23.22ms)
table->string('phone')->unique();
$ php artisan migrate:sync
New column  users->phone  was created

通过执行 SQL 或 数据库可视化工具更改。

$ php artisan migrate:sync
/Users/hai/env/docker/laradock/blog/database/migrations/2014_10_12_000000_create_users_table.php
Add migration column: mobile
Delete migration column: phone

提示

数据库和迁移文件的双向同步,不仅支持字段的增删改,也支持数据表的增删改。

上一篇 下一篇

猜你喜欢

热点阅读