laraib15/laravel-schema-generator
最新稳定版本:v1.2.0
Composer 安装命令:
composer require laraib15/laravel-schema-generator
包简介
Generate Laravel migrations from a schema DSL (create, add, modify, drop columns with DB diffing).
关键字:
README 文档
README
Laravel Schema Generator is a Laravel package that generates migration files using a concise schema DSL (Domain Specific Language).
Based on the DSL and your existing database structure, it determines whether to generate:
- Create table migration
- Add columns migration
- Modify columns migration (DBAL-powered)
- Drop columns migration
- Composite indexes and unique indexes
The generator compares your DSL schema against the actual database schema, making migration generation predictable and automated.
Requirements
| Component | Supported Versions |
|---|---|
| PHP | 8.1+ |
| Laravel | 9, 10, 11, 12 |
| Doctrine DBAL | Optional — Required only for modify-column support |
| Database | MySQL recommended for modify operations |
SQLite does not support
->change(). Modify-column migrations should be executed on MySQL/PostgreSQL.
Installation
composer require laraib15/laravel-schema-generator --dev
Laravel automatically registers the service provider.
The generator command becomes available:
php artisan generate:crud
If you want modify-column (->change()) support, install DBAL in your Laravel app:
composer require doctrine/dbal
Important Notes About Modify-Column Support
Modify-column detection relies on Doctrine DBAL to inspect the actual database schema.
Because of this:
1. Migrations must be executed before DBAL can detect column types
If a migration has not yet been run, the table or column will not exist in the database.
In that case, the generator cannot detect the existing column type and will treat it as a new column, producing:
xxxx_add_columns_to_table.php
instead of:
xxxx_modify_columns_in_table.php
2. Modify operations require:
- Doctrine DBAL installed
- The table/column already existing in the database
Example:
php artisan generate:crud "orders:id:uuid:primary,name:string" php artisan migrate php artisan generate:crud "orders:name:text"
If you skip php artisan migrate, DBAL cannot detect existing columns.
3. SQLite cannot run modify migrations
Even with DBAL installed, SQLite does not support ->change().
Use MySQL/PostgreSQL for production modify support.
Schema DSL Overview
The DSL (Domain-Specific Language) format looks like this:
table:column:type:option1:option2=value,another_column:type:option,...
- Each column definition is separated by commas
- Each option modifies column behavior
Supported Column Types
bigIncrements, bigInteger, binary, boolean,
char, date, datetime, dateTimeTz,
decimal, double, enum, float,
foreignId, geometry, increments, integer,
json, jsonb, longText, mediumText,
morphs, uuidMorphs, smallInteger, tinyInteger,
text, string, set, timestamp, timestampTz,
time, softDeletes, timestamps,
uuid, year
Column Options
| Option | Example | Meaning |
|---|---|---|
| nullable | name:string:nullable |
Adds ->nullable() |
| default | status:string:default=pending |
Sets a default value |
| length | code:string:20 |
Defines string length |
| comment | id:uuid:comment=Primary key |
Adds column comment |
| unique | email:string:unique |
Adds unique index |
| index | category:string:index |
Adds index |
| unsigned | age:integer:unsigned |
Unsigned integer |
| drop | status:string:drop |
Drops a column |
Foreign Keys
Define a foreign key
user_id:foreignId:constrained=users:onDelete=cascade
Generates:
$table->foreignId('user_id') ->constrained('users') ->onDelete('cascade');
Drop a foreign key
user_id:foreignId:drop
Generates:
$table->dropForeign(['user_id']); $table->dropColumn('user_id');
Composite Indexes
Unique Index
unique:user_id|ordered_at
Index
index:category|status
Usage Examples
1. Create a New Table
php artisan generate:crud " orders: id:uuid:primary, user_id:foreignId:constrained=users:onDelete=cascade, status:enum:values=pending|processing|completed:default=pending, amount:decimal:precision=10:scale=2, timestamps "
Generates:
xxxx_create_orders_table.php
2. Add New Columns
php artisan generate:crud "orders:tracking_code:string:nullable"
Generates:
xxxx_add_columns_to_orders_table.php
3. Modify Column Type (DBAL)
php artisan generate:crud "orders:status:text"
Generates:
xxxx_modify_columns_in_orders_table.php
Contains:
$table->text('status')->change();
4. Drop Columns
php artisan generate:crud "orders:tracking_code:string:drop"
Generates:
xxxx_drop_columns_from_orders_table.php
Auto-Diff Logic
The generator compares:
< DSL schema > <--> < DB schema >
Then decides:
| Condition | Output |
|---|---|
| Table does not exist | Create migration |
| Column exists in DSL but not DB | Add column migration |
| Column type or nullability differs | Modify migration |
| Column removed from DSL | Drop migration |
| No differences | No migration created |
Testing
SQLite Tests (Default)
vendor/bin/phpunit
MySQL Integration Tests
Enable MySQL tests:
CRUD_MYSQL_TEST_ENABLED=true vendor/bin/phpunit --group=mysql
Tests cover:
- Modify-column detection
- Doctrine DBAL introspection
- Foreign key dropping
- Combined modify + add sequences
License
MIT License
https://github.com/laraib15
laraib15/laravel-schema-generator 适用场景与选型建议
laraib15/laravel-schema-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mysql」 「postgres」 「laravel」 「crud generator」 「migration generator」 「schema generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laraib15/laravel-schema-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laraib15/laravel-schema-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laraib15/laravel-schema-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Bundle for Doctrine enumerations extension for Postgres
Doctrine extension to manage enumerations in PostgreSQL
Mapper for accessing resources in KWCMS
PHP module for MySql database
Laravel PostgreSQL connection pool
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-15