
WechatIMG1462.jpeg
介紹
使用 laravel 框架的小伙伴,這將會(huì)是你的福音!推薦使用 yuhal/laravel-sync-database,可以幫助您高效開發(fā)哦,歡迎 star OR fork!如果您不想在開發(fā)laravel項(xiàng)目時(shí)手動(dòng)創(chuàng)建遷移文件。它可以幫助您自動(dòng)創(chuàng)建和更新數(shù)據(jù)庫中表結(jié)構(gòu)對(duì)應(yīng)的遷移文件。此外,它還可以與早期同步遷移結(jié)合使用,實(shí)現(xiàn)數(shù)據(jù)庫和遷移文件的雙向同步。
安裝
- 通過 composer 創(chuàng)建項(xiàng)目
$ composer create-project --prefer-dist laravel/laravel blog
- 進(jìn)入 blog 目錄
$ cd blog
- 復(fù)制 .env.example 配置文件,命名為 .env
$ cp .env.example .env
- 修改 .env
# 項(xiàng)目地址
APP_URL=http://blog.com
# mysql數(shù)據(jù)庫連接
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
- composer 設(shè)置國內(nèi)鏡像
$ composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
- composer 安裝依賴包
$ composer install
$ composer require kitloong/laravel-migrations-generator=5.0.1
$ composer require awssat/laravel-sync-migration
$ composer require yuhal/laravel-sync-database
同步
- 首次執(zhí)行遷移
$ 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)
- users 遷移文件新增 phone 字段
table->string('phone')->unique();
- 遷移文件同步數(shù)據(jù)庫
$ php artisan migrate:sync
New column users->phone was created
- users 數(shù)據(jù)庫表重命名 phone 字段為 mobile
通過執(zhí)行 SQL 或 數(shù)據(jù)庫可視化工具更改。
- 數(shù)據(jù)庫同步遷移文件
$ php artisan database: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
提示
數(shù)據(jù)庫和遷移文件的雙向同步,不僅支持字段的增刪改,也支持?jǐn)?shù)據(jù)表的增刪改。