rtcoder/laravel-db-erd
Composer 安装命令:
composer require rtcoder/laravel-db-erd
包简介
Laravel package to generate database ERD diagrams.
README 文档
README
A Laravel package for generating Entity-Relationship Diagrams (ERD) of your database schema. This tool is designed to support multiple database systems like MySQL, PostgreSQL, SQLite, SQL Server, and Oracle.
Features
- Automatically scans your database schema for tables and their relationships.
- Generates ERD diagrams in PDF format using Graphviz.
- Extensible design with support for multiple database drivers.
Installation
Requirements
- PHP 8.1 or higher
- Laravel 10.x or higher
- Graphviz installed on your system
Step 1: Install the package
composer require rtcoder/laravel-db-erd
Step 2: Publish the configuration (optional)
If you need to customize the behavior, publish the configuration file:
php artisan vendor:publish --tag=db-erd-config
If you need to customize the views, you can publish them using:
php artisan vendor:publish --tag=db-erd-views
This will copy the default views to your project's resources/views/vendor/laravel-erd directory, where you can modify them as needed.
Step 3: Install Graphviz
Ensure Graphviz is installed on your system.
On macOS:
brew install graphviz
On Ubuntu:
sudo apt install graphviz
On Windows:
Download and install from Graphviz's official site.
Usage
Generate an ERD Run the following Artisan command to generate the ERD:
php artisan erd:generate --output=storage/erd/erd_diagram.pdf --driver=mysql
The --output option specifies the file path for the generated diagram.
Check Graphviz availability
php artisan erd:doctor
The command verifies that the Graphviz dot binary is available in PATH and prints its detected path and version.
Generate from your application code
use Rtcoder\LaravelERD\Contracts\ERDGeneratorInterface; class ExportSchemaController { public function __invoke(ERDGeneratorInterface $generator): void { $generator->generate(storage_path('erd/schema.svg'), 'pgsql'); } }
You can also resolve the service from the container:
app(ERDGeneratorInterface::class)->generate(storage_path('erd/schema.html'), 'mysql'); app('erd-generator')->generate(storage_path('erd/schema.pdf'), 'pgsql');
Service usage
The package registers the generator in Laravel's service container, so you can generate diagrams from controllers, jobs, commands, scheduled tasks, tests, or any other application service.
Available bindings:
| Binding | Description |
|---|---|
Rtcoder\LaravelERD\Contracts\ERDGeneratorInterface |
Recommended dependency injection target. |
Rtcoder\LaravelERD\Services\ERDGenerator |
Concrete generator implementation. |
erd-generator |
String alias for quick container resolution. |
Controller example:
namespace App\Http\Controllers; use Rtcoder\LaravelERD\Contracts\ERDGeneratorInterface; class ExportSchemaController { public function __invoke(ERDGeneratorInterface $generator): string { $path = storage_path('erd/schema.svg'); $generator->generate($path, 'pgsql'); return $path; } }
Queued job example:
namespace App\Jobs; use Rtcoder\LaravelERD\Contracts\ERDGeneratorInterface; class GenerateSchemaDiagram { public function handle(ERDGeneratorInterface $generator): void { $generator->generate(storage_path('erd/schema.html'), config('erd.default_driver')); } }
Supported Databases
- MySQL
- PostgreSQL
- SQLite
- SQL Server
- Oracle
Available database drivers are represented by the Rtcoder\LaravelERD\Enums\DatabaseConnection enum. PostgreSQL also accepts postgres, postgresql, and psql aliases.
Supported output formats
- SVG
- PNG
- HTML
Available output formats are represented by the Rtcoder\LaravelERD\Enums\OutputFormat enum.
Configuration
You can customize the package by modifying the configuration file (config/erd.php):
return [ 'default_driver' => env('DB_ERD_DRIVER', env('DB_CONNECTION', 'pgsql')), 'output_directory' => storage_path('erd'), 'output_name' => 'erd_diagram', 'output_format' => 'pdf', 'exclude_tables' => ['migrations', 'jobs', 'failed_jobs'], ];
Exceptions
The package exposes concrete exceptions so you can handle generator failures without catching a generic Exception.
| Exception | When it is thrown |
|---|---|
Rtcoder\LaravelERD\Exceptions\UnsupportedOutputFormatException |
The output file extension is not one of pdf, png, svg, or html. |
Rtcoder\LaravelERD\Exceptions\InvalidConnectionNameException |
The selected database driver is not supported. |
Rtcoder\LaravelERD\Exceptions\DirectoryCreationException |
The output directory cannot be created. |
Rtcoder\LaravelERD\Exceptions\TemporaryFileCreationException |
The temporary DOT file for Graphviz cannot be created. |
Rtcoder\LaravelERD\Exceptions\FileWriteException |
The generated DOT or HTML file cannot be written. |
Rtcoder\LaravelERD\Exceptions\GraphvizRenderException |
Graphviz exits with a non-zero status while rendering PDF, PNG, or SVG output. |
Rtcoder\LaravelERD\Exceptions\ViewNotFoundException |
The HTML diagram Blade view cannot be found in the application or package paths. |
Rtcoder\LaravelERD\Exceptions\ERDException |
Base exception for runtime generation failures. |
Example:
use Rtcoder\LaravelERD\Exceptions\ERDException; use Rtcoder\LaravelERD\Exceptions\InvalidConnectionNameException; use Rtcoder\LaravelERD\Exceptions\UnsupportedOutputFormatException; use Rtcoder\LaravelERD\Contracts\ERDGeneratorInterface; try { app(ERDGeneratorInterface::class)->generate(storage_path('erd/schema.svg'), 'pgsql'); } catch (UnsupportedOutputFormatException|InvalidConnectionNameException $exception) { report($exception); } catch (ERDException $exception) { report($exception); }
Troubleshooting
Graphviz not found error
Ensure Graphviz is correctly installed and the dot binary is available in your system's PATH.
You can check this from Laravel with:
php artisan erd:doctor
macOS
brew install graphviz
Ubuntu / Debian
sudo apt install graphviz
Windows
Install Graphviz from the official download page, or use one of the package manager commands below:
winget install graphviz
choco install graphviz
If the command still cannot find dot, restart your terminal and verify that the Graphviz bin directory is included in PATH.
Empty ERD Diagram
Verify that your database schema has relationships (foreign keys).
License
This package is open-source software licensed under the MIT license.
rtcoder/laravel-db-erd 适用场景与选型建议
rtcoder/laravel-db-erd 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rtcoder/laravel-db-erd 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rtcoder/laravel-db-erd 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-08