PHPackages                             egyjs/dbml-to-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. egyjs/dbml-to-laravel

ActiveLibrary[Database &amp; ORM](/categories/database)

egyjs/dbml-to-laravel
=====================

Generate Laravel Eloquent models and migration files directly from your DBML (Database Markup Language) files with ease.

v2.0.00(5mo ago)735.4k↓19.6%3MITJavaScriptPHP ^8.0|^8.1|^8.2|^8.3|^8.4CI passing

Since Nov 17Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/egyjs/dbml-to-laravel)[ Packagist](https://packagist.org/packages/egyjs/dbml-to-laravel)[ Docs](https://github.com/egyjs/dbml-to-laravel)[ GitHub Sponsors](https://github.com/egyjs)[ RSS](/packages/egyjs-dbml-to-laravel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (14)Versions (12)Used By (0)

**DBML to Laravel Eloquent Generator ✨**
========================================

[](#dbml-to-laravel-eloquent-generator-)

**Generate Laravel Eloquent models and migration files directly from your DBML (Database Markup Language) diagrams, accelerating Laravel development and streamlining your schema-to-code workflow.**

> 📰 **Featured on [Laravel News](https://laravel-news.com/dbml-to-laravel)** — the official community blog for Laravel developers!

[![Latest Version on Packagist](https://camo.githubusercontent.com/394f6425f11aa27ad30535b2d79962a52579c35db247796a2638a83ee0195899/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6567796a732f64626d6c2d746f2d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/egyjs/dbml-to-laravel)[![GitHub Tests Action Status](https://camo.githubusercontent.com/433bbc98514a56d4fc6c6f7deb26dca11deb945c016d27d46931abc4d97efa54/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6567796a732f64626d6c2d746f2d6c61726176656c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/egyjs/dbml-to-laravel/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/cea321e8eb92d8efd1915cce31c05ac21797dc8e399ddd10e65c1c534f6f47ce/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6567796a732f64626d6c2d746f2d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/egyjs/dbml-to-laravel)[![Featured on Laravel News](https://camo.githubusercontent.com/fa0965d870814b6e39595cb73a02d9c2de00e9407b061155f362eeb95fc8f17c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2532304e6577732d46656174757265642d6f72616e67653f7374796c653d666c61742d737175617265)](https://laravel-news.com/dbml-to-laravel)

**🚀 Motivation**
----------------

[](#-motivation)

Tired of manually writing Laravel Eloquent models and migration files from your database diagrams? This package automates the process, letting you focus on building features rather than repetitive boilerplate code. Say goodbye to manual schema-to-code conversion and embrace a faster, more efficient Laravel development workflow.

**✨ Features**
--------------

[](#-features)

- **DBML to Laravel Models:** Automatically generate Eloquent models with fillable properties, hidden attributes, and defined relationships (one-to-one, one-to-many, many-to-many).
- **DBML to Migration Files:** Generate clean and accurate migration files with proper data types, nullability, default values, and foreign key constraints, directly from your DBML schema.
- **Customizable Stubs:** Easily modify the default model and migration stubs to align with your project's coding style and specific requirements.
- **Relationship Parsing:** Intelligently parses DBML relationships to create correct Eloquent relationship methods (e.g., `hasMany`, `belongsTo`, `belongsToMany`).
- **Casts Support:** Automatically adds common Eloquent casts (e.g., JSON to arrays, timestamps to datetime objects) based on DBML column types.

[![DBML to Laravel Model & Migration Generator](https://private-user-images.githubusercontent.com/12745270/532518305-d15995b7-b95f-4524-b81d-65007b4549f8.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJp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lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwNDA1VDA0MzExMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWUwODgzNTBmNDFiODA1MmU3M2I5YTRmMzY5NTgwZjI2OTIyZTc3MmZlMDQ3YzcwYTE4MWIxZjgxYzU5ZTdiMTYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.z-_JhdvMSa4p4v2-d5j_u_xorNWb3wkGMQ_-MPDfPCU)

**📦 Installation**
------------------

[](#-installation)

To get started with the DBML to Laravel Eloquent Generator, follow these simple steps:

1. **Require the package via Composer:**

```
composer require egyjs/dbml-to-laravel --dev
```

3. **Publish the customizable stubs (optional, but recommended):**

```
php artisan vendor:publish --tag=dbml-to-laravel-stubs
```

This command will publish the stub files to `stubs/dbml-to-laravel/`, allowing you to customize the generated code.

### **Requirements**

[](#requirements)

- Laravel 8.x+
- PHP 8.0+
- Node.js 18+ runtime (the DBML parser is pre-bundled, so you never need to run `npm install` in your project)

**💡 Usage**
-----------

[](#-usage)

Once installed, you can generate your Laravel models and migrations from a DBML file using the `generate:dbml` Artisan command.

1. **Create your DBML schema file** (e.g., database/schema.dbml).
    **Example database/schema.dbml:**

```
   Table users {
     id int [pk, increment]
     name varchar
     email varchar [unique]
     password varchar
     created_at datetime
     updated_at datetime
   }

   Table posts {
     id int [pk, increment]
     user_id int [ref: > users.id]
     title varchar
     content text
     created_at datetime
     updated_at datetime
   }

   Ref: posts.user_id > users.id

```

3. **Run the Artisan command:**

```
php artisan generate:dbml database/schema.dbml
```

Replace `database/schema.dbml` with the actual path to your DBML file.

### **Expected Output**

[](#expected-output)

After running the command, the package will generate:

- **Eloquent Models:** In your `app/Models` directory (e.g., `app/Models/User.php`).
- **Migration Files:** In your `database/migrations` directory (e.g., `2023_01_01_000000_create_posts_table.php`).

**Example Generated `app/Models/Post.php`:**

```
