Logo
Section 1 / 2

Laravel 迁移设置与实现

了解 Laravel 数据库设置、配置、迁移及修改。

导入

Laravel 迁移设置与实现

了解 Laravel 数据库设置、配置、迁移及修改。

...
phpMyAdmin 预览图

Laravel .env 文件预览

1LOG_CHANNEL=stack 2LOG_DEPRECATIONS_CHANNEL=null 3LOG_LEVEL=debug 4 5DB_CONNECTION=mysql 6DB_HOST=127.0.0.1 7DB_PORT=3306 8DB_DATABASE=laravel 9DB_USERNAME=root 10DB_PASSWORD= 11 ............................
...
通过命令行验证连接

No configuration found for ID: 82638ede07397d38192a42a9d6a58a01f1642e177a3b14e9b4c493b955474128

创建 contacts 表的迁移文件

1php artisan make:migration create_contacts_table

No configuration found for ID: 5fc5d4f3a0f2994377935053ca14fe4a81d67599ac7928f749d5c400ed4409fb

实现迁移约束

1Schema::create('contacts', function (Blueprint $table) { 2 $table->id(); 3 $table->string('phoneNumber', 64)->unique(); 4 $table->text('details')->nullable(); 5 $table->timestamps(); 6});
...
迁移执行示例
...
phpMyAdmin 中迁移结果输出

回滚数据库迁移

1php artisan migrate:rollback

回滚最近三个步骤

1php artisan migrate:rollback --step=3

定义外键迁移

1Schema::table('contacts_info', function (Blueprint $table) { 2 $table->id(); 3 $table->unsignedBigInteger('contact_id'); 4 $table->foreign('contact_id')->references('id')->on('contacts')->onUpdate('cascade')->onDelete('cascade'); 5});

No configuration found for ID: d4c40da4423590ea1064cebf15af6ec9811a851cb5ecb310035f837eb3c9e591

课程简介

Laravel 迁移测验