zuqongtech/laravel-db-introspection 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zuqongtech/laravel-db-introspection

Composer 安装命令:

composer require zuqongtech/laravel-db-introspection

包简介

A Laravel package for automatic database introspection and model generation supporting MySQL, PostgreSQL, SQL Server, and more.

README 文档

README

Packagist Version License Build Status Laravel PHP

A Laravel package for automatic database introspection, model discovery, constraint analysis, and Eloquent model generation. Scans your connected database, analyzes schema metadata, and generates robust Eloquent models — complete with relationships, indexes, PHPDoc, and constraints.

Table of Contents

Features

Category Capability
Database Support Multi-engine support (MySQL, PostgreSQL, SQLite, SQL Server)
Model Generation Auto-generates Eloquent models from live schema
Relationships FK-based detection with optional inverse relationships
Schema Analysis Constraint, index, and integrity analysis
Developer Experience Full PHPDoc generation, dry-run preview, model backups
Configurability Custom paths, namespaces, table filters, and connections

Requirements

  • PHP ^8.2
  • Laravel 10.x or 11.x
  • Composer 2.x

Dependencies

The following locked versions are used in this package. All runtime dependencies carry an MIT license unless noted.

Runtime

Package Version Purpose
laravel/framework v11.46.1 Core Laravel framework
nesbot/carbon 3.10.3 Date/time handling
doctrine/inflector 2.1.0 String inflection for model naming
guzzlehttp/guzzle 7.10.0 HTTP client
monolog/monolog 3.9.0 Logging
ramsey/uuid 4.9.1 UUID generation
league/flysystem 3.30.2 Filesystem abstraction
vlucas/phpdotenv v5.6.2 Environment variable loading
symfony/console v7.3.6 CLI command infrastructure
brick/math 0.14.0 Arbitrary-precision arithmetic

Development

Package Version Purpose
phpunit/phpunit 11.5.44 Test runner
orchestra/testbench v9.15.0 Laravel package testing harness
mockery/mockery 1.6.12 Mock object framework
fakerphp/faker v1.24.1 Fake data generation for tests
laravel/pint v1.27.0 PHP code style fixer
nunomaduro/collision v8.8.2 CLI error reporting
laravel/tinker v2.10.1 Interactive REPL

Laravel 12 Compatibility

This package supports both Laravel 11 and Laravel 12 from the same codebase. No breaking changes are required in your application — only the dependency constraints need updating.

What changed in Laravel 12 that affects this package

Schema::getTableListing() now returns schema-qualified names

In Laravel 12, Schema::getTableListing() returns table names prefixed with the schema name by default (e.g. main.users instead of users). If the package calls this method internally to discover tables, you must strip the schema prefix or pass schemaQualified: false:

// Laravel 11 behaviour (unqualified)
Schema::getTableListing();
// ['migrations', 'users', 'orders']

// Laravel 12 default (qualified)
Schema::getTableListing();
// ['main.migrations', 'main.users', 'main.orders']

// Laravel 12 — opt out of qualification to restore old behaviour
Schema::getTableListing(schemaQualified: false);
// ['migrations', 'users', 'orders']

Grammar and Blueprint constructor signatures changed

Laravel 12 requires a Connection instance to be passed to Illuminate\Database\Grammar and Illuminate\Database\Schema\Blueprint constructors. The old setConnection() method has been removed. If the package instantiates either class directly, update the call:

// Laravel 11
$grammar = new MySqlGrammar;
$grammar->setConnection($connection);

// Laravel 12+
$grammar = new MySqlGrammar($connection);

Carbon 2.x support removed

Laravel 12 drops Carbon 2. This package's lock file already uses Carbon 3.10.3, so no action is needed.

HasUuids now generates UUIDv7

The HasUuids trait switched from UUIDv4 to UUIDv7. This only matters if you rely on UUID ordering in generated models. No change is required for the generator itself.

Upgrading the package to support Laravel 12

1. Update composer.json constraints

{
    "require": {
        "php": "^8.2",
        "laravel/framework": "^11.0|^12.0"
    },
    "require-dev": {
        "orchestra/testbench": "^9.0|^10.0",
        "phpunit/phpunit": "^11.0|^12.0",
        "pestphp/pest": "^2.0|^3.0"
    }
}

2. Expand the CI matrix

Add Laravel 12 alongside Laravel 11 in your GitHub Actions workflow:

# .github/workflows/tests.yml
strategy:
  matrix:
    php: [8.2, 8.3, 8.4]
    laravel: ['11.*', '12.*']
    include:
      - laravel: '11.*'
        testbench: '9.*'
      - laravel: '12.*'
        testbench: '10.*'
    exclude:
      - php: 8.1
        laravel: '11.*'
      - php: 8.1
        laravel: '12.*'

3. Guard schema-qualified table names

If the package uses Schema::getTableListing(), add a version guard to normalise output across both framework versions:

use Illuminate\Support\Facades\Schema;

$tables = version_compare(app()->version(), '12.0.0', '>=')
    ? Schema::getTableListing(schemaQualified: false)
    : Schema::getTableListing();

4. Run the full test suite against both versions

# Test against Laravel 11
composer require laravel/framework:^11.0 orchestra/testbench:^9.0 --no-update
composer update && composer test

# Test against Laravel 12
composer require laravel/framework:^12.0 orchestra/testbench:^10.0 --no-update
composer update && composer test

Version support matrix

Package version Laravel 11 Laravel 12 PHP
1.x (current) ^8.2
2.x (planned) ^8.2

Laravel 11 security support ended March 12, 2026. Laravel 12 receives bug fixes until August 2026 and security fixes until February 2027.

Installation

Install via Composer:

composer require zuqongtech/laravel-db-introspection

For local development:

git clone https://github.com/zuqongtech/laravel-db-introspection.git
cd laravel-db-introspection
composer install

Configuration

Publish the configuration file:

php artisan vendor:publish \
  --provider="Zuqongtech\LaravelDbIntrospection\LaravelDbIntrospectionServiceProvider" \
  --tag=config

This creates config/zt-introspection.php:

return [
    'output_path'   => app_path('Models'),
    'namespace'     => 'App\\Models',
    'ignore_tables' => [],
];

Usage

Basic Usage

php artisan zt:generate-models

Available Flags

Model Generation

Flag Description Example
--force Overwrite existing models without prompting --force
--backup Back up existing models before overwriting --backup
--dry-run Preview actions without writing any files --dry-run
--namespace= Override the namespace for generated models --namespace="App\\Domain\\Models"
--path= Override the output directory --path=modules/Core

Table Selection & Filtering

Flag Description Example
--tables=* Generate models only for the specified tables --tables=users --tables=orders
--ignore=* Exclude specific tables from generation --ignore=migrations
--connection= Use a specific database connection --connection=pgsql

Documentation & Metadata

Flag Description Example
--with-phpdoc Add PHPDoc blocks for IDE autocompletion --with-phpdoc
--with-constraints Embed constraint details in model comments --with-constraints

Relationships

Flag Description Example
--with-inverse Generate inverse relations (hasMany, hasOne) --with-inverse
--validate-fk Validate all foreign key references exist --validate-fk

Constraint & Integrity Analysis

Flag Description Example
--analyze-constraints Display a summary of PKs, FKs, and indexes --analyze-constraints
--validate-fk Validate FK integrity across all tables --validate-fk
--show-recommendations Show schema optimization suggestions --show-recommendations

Example Commands

Generate models with PHPDoc and inverse relationships:

php artisan zt:generate-models --with-phpdoc --with-inverse

Generate models for specific tables only:

php artisan zt:generate-models --tables=users --tables=orders

Validate foreign keys and analyze constraints:

php artisan zt:generate-models --validate-fk --analyze-constraints

Run a full analysis with optimization recommendations:

php artisan zt:generate-models --analyze-constraints --show-recommendations

Preview what would be generated without writing files:

php artisan zt:generate-models --dry-run

Generated Output

Models are written to app/Models/ by default:

app/
└── Models/
    ├── User.php
    ├── Order.php
    └── Product.php

Each generated model includes:

  • $table — explicit table binding
  • $fillable — all column names
  • $primaryKey — including composite key support
  • Soft delete detection (SoftDeletes trait)
  • Timestamp detection ($timestamps)
  • Relationships from foreign keys
  • Optional inverse relationships
  • Optional PHPDoc property blocks
  • Optional constraint annotations

Example generated model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Orders extends Model
{
    protected $table = 'orders';

    protected $fillable = [
        'id',
        'user_id',
        'product_id',
        'status',
        'created_at',
        'updated_at',
    ];

    public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

Development

Clone and install dependencies:

git clone https://github.com/zuqongtech/laravel-db-introspection.git
cd laravel-db-introspection
composer install

Run the test suite:

composer test

Pull requests with tests and a clean commit history are welcome.

Contributing

Bug reports and feature requests can be submitted via GitHub Issues.

When submitting a pull request:

  • Include tests for any new behaviour
  • Keep commits focused and well-described
  • Follow PSR-12 coding standards

Credits

Developed and maintained by Gideon Zozingao / Zuqongtech.

© 2025 Gideon Zozingao. Licensed under the MIT License.

zuqongtech/laravel-db-introspection 适用场景与选型建议

zuqongtech/laravel-db-introspection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 107 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 zuqongtech/laravel-db-introspection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zuqongtech/laravel-db-introspection 我们能提供哪些服务?
定制开发 / 二次开发

基于 zuqongtech/laravel-db-introspection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 107
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-12