Automatically generate ERD Diagrams from Model's relations in Laravel

Overview

Laravel ERD Generator

Automatically generate interactive ERD from Models relationships in Laravel.

  • This package provides a CLI to automatically generate interactive ERD by looking into relationships setup inside Models.
  • The tool's interface is available to get JSON data of relationships and table schema from Models to be used by visual charts libraries of your choice such as d3.js etc
  • Output is an interactive ERD (Entity Relation Diagram) powered by HTML and JS (GOJS).
Lang Link
Details Medium Article
Demo Online Demo

Requirements

Lang Version
PHP 7.4 or 8.0
Laravel 6.* or 8.*

Installation

You can install the package via composer:

composer require kevincobain2000/laravel-erd --dev

You can publish the config file with:

php artisan vendor:publish --provider="Kevincobain2000\LaravelERD\LaravelERDServiceProvider"

Usage

You can access the ERD in localhost:3000/erd

or generate a static HTML

php artisan erd:generate

ERD HTML is generated inside docs/.

Sample

Screenshot

Image

Get JSON output

use Kevincobain2000\LaravelERD\LaravelERD;

$modelsPath = base_path('app/Models');

$laravelERD = new LaravelERD();
$linkDataArray = $laravelERD->getLinkDataArray($modelsPath);
$nodeDataArray = $laravelERD->getNodeDataArray($modelsPath);
$erdData = json_encode(
    [
        "link_data" => $linkDataArray,
        "node_data" => $nodeDataArray,
    ],
    JSON_PRETTY_PRINT
);
var_dump($erdData);

Sample JSON output

{
    "link_data": [
        {
            "from": "comments",
            "to": "users",
            "fromText": "1..1\nBT",
            "toText": "",
            "fromPort": "author_id",
            "toPort": "id",
            "type": "BelongsTo"
        },
        {
            "from": "comments",
            "to": "posts",
            "fromText": "1..1\nBT",
            "toText": "",
            "fromPort": "post_id",
            "toPort": "id",
            "type": "BelongsTo"
        },
        ...
        ...
    ],
    "node_data": [
        {
            "key": "comments",
            "schema": [
                {
                    "name": "id",
                    "isKey": true,
                    "figure": "Hexagon",
                    "color": "#be4b15",
                    "info": "integer"
                },
                {
                    "name": "author_id",
                    "isKey": false,
                    "figure": "Decision",
                    "color": "#6ea5f8",
                    "info": "integer"
                },
                ...
                ...
        }
        ...
    ]

Testing

./vendor/bin/phpunit

Changelog

  • Initial Release - POC
Comments
  • Support glob models path

    Support glob models path

    In case some project has modular folder design for Models, it would be good to support glob models path. But since the config requires namespace, it can not make dynamic path working.

    So what I have changed is to dynamically get namespace for each files. And then we can remove the config of namespace.

    opened by koshuang 3
  • Disable route

    Disable route

    I only want to generate HTML during my build process. I do not want my app to serve the ERD diagram, so it would be nice if I could disable the route.

    Right now I sort of can by adding a middleware to deny access -- but that feels like kind of a hack. Maybe setting up the route could be skipped if I change it to null in the config?

    opened by nie7321 2
  • How to use the generator with modular folder structure

    How to use the generator with modular folder structure

    The project I try to generate the diagram for is using the modular folder structure.

    ├── app
    │   ├── Modules
    │   │   ├── Module1
    │   │   │   ├── Models
    │   │   │   │   ├── ModuleOneModelOne.php
    │   │   │   │   └── ModuleOneModelTwo.php
    

    How can I configure the package in the way to retrieve the models from this stucture ? Thank you in advance.

    opened by allanvb 2
  • Collaborate?

    Collaborate?

    Hi, cool stuff here!

    How about basing it off https://github.com/Lt-Mayonesa/wwwsqldesigner or the original https://github.com/ondras/wwwsqldesigner

    I managed to turn it into a package already, now working on the relationships, but a little lost. This guy here has some incomplete ideas about relationships http://biodesignrealworld.github.io/LaravelDatabaseDesigner/#

    What do you think?

    opened by bilogic 1
  • Fix routes registering.

    Fix routes registering.

    Currently, routes registering is not working nicely since config('laravel-erd.url') is not readed correctly on service provider:

    <?php
    
    namespace Kevincobain2000\LaravelERD;
    
    use Spatie\LaravelPackageTools\Package;
    use Spatie\LaravelPackageTools\PackageServiceProvider;
    use Kevincobain2000\LaravelERD\Commands\LaravelERDCommand;
    use Route;
    
    class LaravelERDServiceProvider extends PackageServiceProvider
    {
        public function configurePackage(Package $package): void
        {
            $package
                ->name('laravel-erd')
                ->hasConfigFile('laravel-erd')
                ->hasViews()
                ->hasCommand(LaravelERDCommand::class);
    
            // TRY TO LOG VALUE OF config('laravel-erd.url') HERE, IT WILL BE NULL.
            
            Route::get(config('laravel-erd.url'), [\Kevincobain2000\LaravelERD\Controllers\LaravelERDController::class, 'index'])
                ->name('laravel-erd.index')
                ->middleware(config('laravel-erd.middlewares'));
        }
    }
    

    This PR introduces routes configuration as documented by spatie/laravel-package-tools to fix route registering.

    opened by dfsmania 1
  • Makes routing property configurable

    Makes routing property configurable

    This PR turns the routing property into a configurable parameter in the package.

    The AvoidsNodes option has extremely bad performance for my ER diagram. That seems like it might be something people run into often, so maybe this will be helpful? :-)

    Although I don't know if you'd prefer to have folks customize the view that gets put in their app instead of making a bunch of config options. I'm also finding that I need to change the layout to LayeredDigraphLayout to make the diagram usable for my app.

    opened by nie7321 0
  • Misisng Tables added via Traits

    Misisng Tables added via Traits

    I'm using Laravel Sanctum and noticed the personal_access_tokens table didn't show up in this brilliant ERD. I believe it uses a MorphMany relationship.

    namespace App\Models\Organization;
    
    class Organization {
      use Laravel\Sanctum\HasApiTokens;
      use HasApiTokens;
    }
    

    Just a thought.

    Thanks for making this package!

    opened by philsturgeon 2
  • Problem Installing

    Problem Installing

    Greetings.

    I am a new programmer. Just wondering if anyone else has a problem with installation. REALLY want to use laravel-erd!

    composer require kevincobain2000/laravel-erd --dev

    spatie/laravel-package-tools[1.11.0, ..., 1.11.3] require illuminate/contracts ^7.0|^8.0|^9.0 -> found illuminate/contracts[v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev, v9.0.0-beta.1, ..., 9.x-dev] but these were not loaded, likely because it conflicts with another require

    "require": { "php": "^8.0.18", "barryvdh/laravel-debugbar": "^3.5", "diglactic/laravel-breadcrumbs": "^6.0", "doctrine/dbal": "2.*", "fideloper/proxy": "^4.0", "google/apiclient": "^2.0", "laravel-notification-channels/webpush": "^5.1", "laravel/framework": "^6.18.35", "laravel/helpers": "^1.4", "laravel/tinker": "^2.0", "maatwebsite/excel": "^3.1", "nesbot/carbon": "^2.41", "robicch/jquery-gantt": "^6.3", "spatie/laravel-google-calendar": "^3.2" }, "require-dev": { "facade/ignition": "^1.4", "fzaninotto/faker": "^1.9.1", "laravel/ui": "^1.0", "mockery/mockery": "^1.0", "nunomaduro/collision": "^3.0", "phpunit/phpunit": "^8.0" },

    Can laravel-erd be used with this setup do you think?

    Nick

    opened by Boy19991 1
Generate trends for your models. Easily generate charts or reports.

Laravel Trend Generate trends for your models. Easily generate charts or reports. Support us Like our work? You can support us by purchasing one of ou

Flowframe 139 Dec 27, 2022
A laravel package to handle cascade delete and restore on model relations.

Laravel Model Soft Cascade A laravel package to handle cascade delete and restore on model relations. This package not only handle the cascade delete

Touhidur Rahman 18 Apr 29, 2022
A simple laravel package to enable cascade deleting on polymorphic relations.

Morph Cascade Delete A simple laravel package to enable cascade deleting on polymorphic relations. Installation Install with composer composer requi

Moataz Hajres 18 May 15, 2022
Automatically validating Eloquent models for Laravel

Validating, a validation trait for Laravel Validating is a trait for Laravel Eloquent models which ensures that models meet their validation criteria

Dwight Watson 955 Dec 25, 2022
Record created by, updated by and deleted by on Eloquent models automatically.

quarks/laravel-auditors Record created by, updated by and deleted by (if SoftDeletes added) on Eloquent models automatically. Installation composer re

Quarks 3 Jun 13, 2022
Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.

Laravel Migrations Generator Generate Laravel Migrations from an existing database, including indexes and foreign keys! This package is cloned from ht

Kit Loong 1.4k Jan 1, 2023
The package lets you generate TypeScript interfaces from your Laravel models.

Laravel TypeScript The package lets you generate TypeScript interfaces from your Laravel models. Introduction Say you have a model which has several p

Boris Lepikhin 296 Dec 24, 2022
🏭 An easy way to generate populated factories for models.

Laravel Populated Factory provides an easy way to generate populated factories for models according to types & names of their columns. Install You can

Coderello 241 Nov 25, 2022
A package to generate YouTube-like IDs for Eloquent models

Laravel Hashids This package provides a trait that will generate hashids when saving any Eloquent model. Hashids Hashids is a small package to generat

Λгi 25 Aug 31, 2022
Generate previous attributes when saving Eloquent models

This package provides a trait that will generate previous attributes when saving any Eloquent model.

Ricardo Sawir 33 Nov 6, 2022
Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Jameson Lopp 28 Dec 19, 2022
Automatically disable Google's FLoC in Laravel apps

Automatically disable Google's FLoC in Laravel apps This package will automatically disable Google's FLoC. Support us We invest a lot of resources int

Spatie 68 Oct 21, 2022
Automatically load your helpers in your laravel application.

Laravel AutoHelpers Automatically load your helpers in your laravel application. Installation You can install the package via composer: composer requi

Florian Wartner 6 Jul 26, 2021
Automatically encrypt and decrypt Laravel 5 Eloquent values

Eloquent Encryption/Decryption for Laravel 5 Automatically encrypt and decrypt Laravel 5 Eloquent values. READ THIS FIRST Encrypted values are usually

Del 85 Mar 19, 2022
Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string template and recompiles it if needed.

Laravel-fly-view Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string temp

John Turingan 16 Jul 17, 2022
Searches for multilingual phrases in Laravel project and automatically generates language files for you.

Laravel Lang Generator Searches for multilingual phrases in a Laravel project and automatically generates language files for you. You can search for n

Gleb 5 Oct 19, 2022
Simple address and contact management for Laravel with automatically geocoding to add longitude and latitude

Laravel Addresses Simple address and contact management for Laravel with automatically geocoding to add longitude and latitude. Installation Require t

Chantouch Sek 2 Apr 4, 2022
Package to optimize your site automatically which results in a 35%+ optimization

Laravel Page Speed Simple package to minify HTML output on demand which results in a 35%+ optimization. Laravel Page Speed was created by Renato Marin

Renato Marinho 2.2k Dec 28, 2022
A tool to automatically fix PHP Coding Standards issues by Dragon Code.

The Dragon Code Styler Installation Required PHP: ^8.0 Composer: ^2.0 Locally composer global require dragon-code/codestyler Usage When you run the co

The Dragon Code 24 Aug 27, 2022