企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## **Laravel5.5 数据迁移和填充** ##### <blockquote class="danger"><p>出现的原因:在一个多人开发的项目中,你的同事修改了某个数据库结构并修改了代码,通过 git 你可以即时的同步同事修改的代码,但是数据库结构,你只能通过手工的方式来复制同事修改的 SQL 语句,执行以保证数据库的结构一致。那么,Laravel 中的数据库迁移概念,就是用于解决团队中保证数据库结构一致的方案</p></blockquote> * [ ] 数据的迁移 1. 创建数据表:包含三个操作 1.1:创建迁移表文件[路径位置:\database\migrations\,如果是第一次执行,也会生成migrations表],创建迁移表文件命令: php artisan make:migration create_test_table --create=test ![](https://img.kancloud.cn/17/f2/17f2413b8422ad69b0279fa8e58c8a73_1110x69.png) ![](https://img.kancloud.cn/be/07/be07a9fec336d80a14ef830017cb19f2_321x156.png) ![](https://img.kancloud.cn/3c/44/3c44666c75c2d74f9d3a0cbf54c69cf5_582x53.png) ![](https://img.kancloud.cn/0d/43/0d43b70e6b9eb62bfd24becad94e3fab_1484x881.png) 1.2:创建数据表文件[会生成test表],创建迁移表文件命令:php artisan migrate [可以指定迁移某个文件:php artisan migrate --path=/database/migrations/2023_08_04_101638_create_ht_user_logs_table.php] ![](https://img.kancloud.cn/58/59/585973233585e8e0a23b7293a5c9b7a2_736x205.png) ![](https://img.kancloud.cn/29/81/29812f4d6d6b03f72104553a52ece300_157x46.png) ![](https://img.kancloud.cn/0c/0a/0c0a4c28a060dc21e7a322d7983e3f0d_1530x213.png) 2. 重命名表名称 2.1:创建重命名表的迁移文件,命令:php artisan make:migration rename_test_to_tests --table test ![](https://img.kancloud.cn/55/ec/55ec2a9ed73dd426500e683e327da816_1113x50.png) ![](https://img.kancloud.cn/31/c4/31c470ada25be97cefa6b2a0987fe5c3_1187x701.png) ![](https://img.kancloud.cn/07/74/0774d150fccb032ba5620dadab1f2dbe_755x246.png) ![](https://img.kancloud.cn/ac/b6/acb6d0e1fffc975521a9b5c068808897_590x134.png) ![](https://img.kancloud.cn/0e/ab/0eabf8a1a5234e6895600ffb7b928b95_113x43.png) ![](https://img.kancloud.cn/46/e6/46e65696d0e99229f6f0c5448e9ca627_1495x188.png) 3. tests表中增加头像(avatar)字段 3.1:新加一个迁移文件,命令:php artisan make:migration add_avatar_into_tests ![](https://img.kancloud.cn/21/71/2171215cefae5e8c9222fa7611de7da9_1009x69.png) ![](https://img.kancloud.cn/a8/bd/a8bd19c163efaa8bfc5fecd78ec21b73_1498x756.png) ![](https://img.kancloud.cn/a4/3c/a43cf49e81ba5b1b30b2f95ad3898385_987x230.png) ![](https://img.kancloud.cn/fc/9a/fc9ae80a29973e23d0c91e44ace1c4e8_740x188.png) ![](https://img.kancloud.cn/4c/c8/4cc8320d7fa29e26ee5c726d8ff02cf1_1544x223.png) 4. 删除数据表 4.1:在迁移文件中,down方法中编写内容:Schema::dropIfExists('tests'); ![](https://img.kancloud.cn/b4/75/b475c12f903ec6a084743ab7c8e91c67_1537x674.png) 4.2:执行命令:php artisan migrate:rollback ![](https://img.kancloud.cn/dc/d0/dcd0c917752a8a09e518166cc3475795_815x271.png) ![](https://img.kancloud.cn/d8/9e/d89eb8518c7e046a1b75256193ba8c86_674x135.png) 表:tests也被删除了,无法截图 * [ ] 数据的填充 * [ ] 注意:有时间执行填充命令,会提示:填充文件不存在问题,但是填充文件其实是存在,这个时候需要使用命令:composer dump-autoload重新加载一次,再执行填充命令即可 ![](https://img.kancloud.cn/2e/43/2e43326c957f7fd0bfff858fe240ec17_1346x588.png) ![](https://img.kancloud.cn/1a/33/1a336a96d3a425d217acc6eccd9f70ec_1058x191.png) 1. 数据填充分类两种方式:编写seeder填充数据与使用工厂填充数据 <span style="color:yellow;background-color:red">1.1:seeder填充数据</span> 1.1.1:seeder填充数据:进入到项目根目录运行Artisan命令,生成Seeder类。文件都被放置在 database/seeds 目录下,命令: ``` php artisan make:seeder TestsTableSeeder ``` ![](https://img.kancloud.cn/c4/44/c444fca5369a99f5a9b568fb96b3c3b2_926x47.png) 1.1.2:修改填充文件:DatabaseSeeder.php 和 TestsTableSeeder.php DatabaseSeeder.php ![](https://img.kancloud.cn/a8/9b/a89b24a6e8d3e039a0bc3bcce981283c_1480x502.png) TestsTableSeeder.php ![](https://img.kancloud.cn/3b/d0/3bd0836db5111e080d0d7cbc2d497449_1776x637.png) 1.1.3:执行填充,将会把所有填充文件中的数据插入数据库,填充命令: ``` php artisan db:seed【使用了DatabaseSeeder.php】 ``` ``` php artisan db:seed --class=TestsTableSeeder【可以不使用DatabaseSeeder.php,直接调用】 ``` <span style="color:red">注释:如果上述填充命令:php artisan db:seed 执行后,数据填充不成功,则先执行命令</span>: ``` composer dump-autoload ``` ![](https://img.kancloud.cn/9e/f9/9ef91ccbe8d141920da0baa495e0375c_792x208.png) ![](https://img.kancloud.cn/bf/fb/bffba9c33bead4b4fa1e4fd211653372_1637x253.png) <span style="color:yellow;background-color:red">1.2:工厂填充数据</span> 1.2.1:工厂填充数据:创建工厂模型(路径:/database/factories/)创建TestsFactory.php工厂类,内容随便写,如图所示: 创建测试模型:TestsModel.php ![](https://img.kancloud.cn/c3/7e/c37e6d460900356d1ae5f91691669e89_1576x898.png) 创建测试工厂类:TestsFactory.php (复制或使用命令都可以) ![](https://img.kancloud.cn/13/0b/130b6f188d0fa22a48d51dd9afb92f94_1427x681.png) 创建TestsTableSeeder填充文件 (复制或使用命令都可以) ![](https://img.kancloud.cn/a0/22/a022fd6485428c8ceeaf6127fc46c65c_1802x796.png) 在DatabaseSeeder.php类文件中调用填充文件 ![](https://img.kancloud.cn/ee/fa/eefaa2f010faf3784f877a41747b491e_1483x597.png) 在根目录执行命令:php artisan db:seed 或者 特定的:php artisan db:seed --class=TestsTableSeeder ![](https://img.kancloud.cn/fc/a2/fca2d380fddaf08cd648a233a79f29ce_1006x376.png)