搜索

📄 文章 📚 合集
热门搜索
🐘 PHP ⚡ Laravel 🎨 Vue.js ⚛️ React 📦 Yii 📘 JavaScript 🗄️ MySQL 🐳 Docker
返回合集

「好站站」企业建站引擎数据库迁移

代码示例
//迁移是 Laravel 提供的数据库版本控制系统,用 PHP 代码代替 SQL 语句来管理数据库结构

数据库迁移包含的数据库的设计编写和执行
#1. 设计    确定有哪些表、哪些字段、什么类型    在脑子里或纸上设计 
#2. 编写    写成迁移文件(PHP 代码)    把设计变成 up() 方法   
#3. 执行    php artisan migrate    把代码变成真实的数据库表    


数据库迁移的操作流程明细

1.生成迁移文件
在项目根目录 D:\laragon\www\engine-api 下执行:
php artisan make:migration create_sites_table
php artisan make:migration create_pages_table
php artisan make:migration create_page_components_table
生成的文件位于 database/migrations/ 目录下

2、编写迁移文件内容
(1) create_sites_table.php(站点配置表)
 public function up(): void
  {
    Schema::create('sites', function (Blueprint $table) {
      $table->id();
        $table->string('site_name', 255)->default('好站站企业官网')->comment('网站名称');
        table->string('site_logo', 500)->default('/logo.png')->comment('LOGO路径,相对于public目录');
        $table->string('site_keywords', 500)->default('企业建站,可视化建站,拖拽建站,好站站,企业官网,建站引擎')->comment('SEO关键词');
        $table->text('site_description')->nullable()->comment('SEO描述');
        $table->timestamps();
    });
  }
(2)create_pages_table.php(页面管理表)
 public function up(): void
    {
     Schema::create('pages', function (Blueprint $table) {
      $table->id();
      $table->foreignId('site_id')->constrained()->onDelete('cascade')->comment('关联站点ID');
      $table->string('title', 255)->default('新页面')->comment('页面标题');
      $table->string('slug', 255)->comment('访问别名,如:index、about');
      $table->boolean('is_home')->default(false)->comment('是否为首页:1是0否');
      $table->boolean('status')->default(false)->comment('发布状态:0草稿 1已发布');
      $table->timestamps();
    });
  }
(3). create_page_components_table.php(页面组件表)
 public function up(): void{
    Schema::create('page_components', function (Blueprint $table) {
      $table->id();
      $table->foreignId('page_id')->constrained()->onDelete('cascade')->comment('关联页面ID');
      $table->string('component_type', 50)->comment('组件类型');
      $table->json('content')->nullable()->comment('组件内容');
      $table->json('settings')->nullable()->comment('样式设置');
      $table->integer('sort_order')->default(0)->comment('排序');
      $table->timestamps();
    });
  }
    
3、执行迁移回滚
(如果之前执行过)
php artisan migrate:rollback
# 执行迁移 php artisan migrate
    
4.最终结果
NFO  Running migrations.
0001_01_01_000000_create_users_table ...... 115.42ms DONE
0001_01_01_000001_create_cache_table ...... 28.93ms DONE
0001_01_01_000002_create_jobs_table ...... 82.14ms DONE
2026_04_16_072807_create_sites_table ...... 15.70ms DONE
2026_04_16_072826_create_pages_table ...... 78.81ms DONE
2026_04_16_072852_create_pages_components_table ...... 62.50ms DONE


🧸 adorable code

专注 PHP、JavaScript、Laravel、Vue.js、React、Yii 全栈开发。记录技术探索过程中的灵感与经验,分享工程实践洞见。

hello@adorablecode.com

23