Local documentation

Install, configure, and generate ERD diagrams.

This page mirrors the package README in a browsable local format, with the essentials grouped for quick setup inside a Laravel project.

Features

Schema scanning Reads tables and foreign key relationships from the configured database connection.
Graphviz exports Generates PDF, PNG, and SVG diagrams using the local Graphviz dot command.
Interactive HTML Can render a browser-friendly diagram view through the package Blade template.
Laravel integration Provides package auto-discovery, publishable config and views, plus an Artisan command.

Installation

Requirements

  • PHP 8.1 or higher
  • Laravel 10.x or higher
  • Graphviz installed locally for PDF, PNG, and SVG exports

Install the package

composer require rtcoder/laravel-db-erd

Publish configuration

php artisan vendor:publish --tag=db-erd-config

Publish views

php artisan vendor:publish --tag=db-erd-views
Published views are copied to resources/views/vendor/laravel-erd, so you can customize the generated HTML diagram without editing vendor files.

Usage

Generate a diagram with the erd:generate Artisan command. The output extension controls the renderer.

php artisan erd:generate --output=storage/erd/erd_diagram.pdf --driver=mysql

HTML output

php artisan erd:generate --output=storage/erd/erd_diagram.html --driver=pgsql

Check Graphviz

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 code

use Rtcoder\LaravelERD\Contracts\ERDGeneratorInterface;

class ExportSchemaController
{
    public function __invoke(ERDGeneratorInterface $generator): void
    {
        $generator->generate(storage_path('erd/schema.svg'), 'pgsql');
    }
}

Resolve from container

app(ERDGeneratorInterface::class)->generate(storage_path('erd/schema.html'), 'mysql');
app('erd-generator')->generate(storage_path('erd/schema.pdf'), 'pgsql');

Config defaults

When --output, --driver, or --format are omitted, the command reads defaults from config/erd.php.

Service API

The package registers the generator in Laravel's service container, so diagrams can be generated from controllers, jobs, scheduled tasks, tests, or custom commands.

ERDGeneratorInterface Recommended dependency injection target.
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 Drivers And Formats

Databases MySQL, PostgreSQL, SQLite, SQL Server, and Oracle through the DatabaseConnection enum.
Formats PDF, SVG, PNG, and HTML through the OutputFormat enum.

PostgreSQL accepts pgsql, postgres, postgresql, and psql connection names.

Configuration

After publishing the config, customize config/erd.php for your application.

<?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 exception classes so applications can react to specific generator failures.

UnsupportedOutputFormatException The output file extension is not pdf, png, svg, or html.
InvalidConnectionNameException The selected database driver is not supported.
DirectoryCreationException The output directory cannot be created.
TemporaryFileCreationException The temporary DOT file for Graphviz cannot be created.
FileWriteException The generated DOT or HTML file cannot be written.
GraphvizRenderException Graphviz exits with a non-zero status while rendering PDF, PNG, or SVG output.
ViewNotFoundException The HTML diagram Blade view cannot be found in the application or package paths.
ERDException Base exception for runtime generation failures.

Example handling

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

Install Graphviz for your operating system and make sure the dot binary is available in your system path.

php artisan erd:doctor

macOS

brew install graphviz

Ubuntu / Debian

sudo apt install graphviz

Windows

Use the official Graphviz installer, Windows Package Manager, or Chocolatey.

winget install graphviz
choco install graphviz

After installation, restart your terminal and confirm that the Graphviz bin directory is available in PATH.

Empty diagram

Check that the selected database has tables with foreign keys and that the command uses the expected driver.

php artisan erd:generate --format=html --driver=mysql