PHPackages                             robinncode/laravel-db-craft - 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. robinncode/laravel-db-craft

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

robinncode/laravel-db-craft
===========================

Generate Laravel migrations and seeders from existing database connections

1.0.3(6mo ago)08MITPHPPHP ^8.0|^8.1|^8.2|^8.3

Since Nov 1Pushed 6mo agoCompare

[ Source](https://github.com/robinNcode/lara-db-craft)[ Packagist](https://packagist.org/packages/robinncode/laravel-db-craft)[ Docs](https://github.com/robinNcode/lara-db-craft)[ RSS](/packages/robinncode-laravel-db-craft/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

Laravel DB Craft
================

[](#laravel-db-craft)

[![Latest Version on Packagist](https://camo.githubusercontent.com/751c5caea27885e0e763db1f0cb2f5a419a5985ebe4982c515059e3ed199ec04/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f62696e6e636f64652f6c6172612d64622d63726166742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robinncode/lara-db-craft)[![Total Downloads](https://camo.githubusercontent.com/8741278ee4ecad22fdccb5cc33c7a1efd41de335a4b6392e440ba08463fbfa94/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f62696e6e636f64652f6c6172612d64622d63726166742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robinncode/lara-db-craft)

Laravel DB Craft is a powerful package that automatically generates migration and seeder files from your existing database connections. Perfect for reverse engineering databases, creating backups, or migrating from legacy systems.

Features
--------

[](#features)

- ✨ **Automatic Migration Generation**: Generate Laravel migration files from existing database tables
- 🔄 **Seeder Generation**: Create seeders with actual table data
- 🎯 **Table-Specific Generation**: Generate for all tables or specific ones
- 🗄️ **Multi-Database Support**: Works with MySQL, PostgreSQL, and SQLite
- 🔗 **Foreign Key Detection**: Automatically detects and includes foreign key constraints
- 📊 **Index Support**: Preserves indexes and unique constraints
- ⚙️ **Configurable**: Exclude tables, customize paths, and more
- 🚀 **Easy to Use**: Simple artisan commands

Requirements
------------

[](#requirements)

- PHP 8.0 or higher
- Laravel 9.x, 10.x, or 11.x
- Database connection (MySQL, PostgreSQL, or SQLite)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require robinncode/laravel-db-craft
```

The package will automatically register its service provider.

Optionally, publish the configuration file:

```
php artisan vendor:publish --provider="RobinNcode\LaravelDbCraft\DbCraftServiceProvider"
```

This will create a `config/db-craft.php` file where you can customize the package behavior.

Usage
-----

[](#usage)

### Generate Migrations

[](#generate-migrations)

#### Generate migrations for all tables:

[](#generate-migrations-for-all-tables)

```
php artisan get:migration
```

#### Generate migration for a specific table:

[](#generate-migration-for-a-specific-table)

```
php artisan get:migration users
```

#### Use a specific database connection:

[](#use-a-specific-database-connection)

```
php artisan get:migration --connection=mysql
```

### Generate Seeders

[](#generate-seeders)

#### Generate seeders for all tables with data:

[](#generate-seeders-for-all-tables-with-data)

```
php artisan get:seeder
```

#### Generate seeder for a specific table:

[](#generate-seeder-for-a-specific-table)

```
php artisan get:seeder users
```

#### Use a specific database connection:

[](#use-a-specific-database-connection-1)

```
php artisan get:seeder --connection=mysql
```

Configuration
-------------

[](#configuration)

After publishing the config file, you can customize the following options in `config/db-craft.php`:

```
return [
    // Database connection to use
    'connection' => env('DB_CRAFT_CONNECTION', null),

    // Path where migration files will be stored
    'migrations_path' => database_path('migrations'),

    // Path where seeder files will be stored
    'seeders_path' => database_path('seeders'),

    // Tables to exclude from generation
    'exclude_tables' => [
        'migrations',
        'password_resets',
        'password_reset_tokens',
        'failed_jobs',
        'personal_access_tokens',
    ],

    // Number of records per chunk in seeders
    'seeder_chunk_size' => 100,
];
```

Examples
--------

[](#examples)

### Reverse Engineering an Existing Database

[](#reverse-engineering-an-existing-database)

If you have an existing database and want to create Laravel migrations:

```
# Connect to your existing database in .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_existing_db
DB_USERNAME=root
DB_PASSWORD=secret

# Generate all migrations
php artisan get:migration

# Generate all seeders
php artisan get:seeder
```

### Backing Up Table Data

[](#backing-up-table-data)

Create seeders to backup your current data:

```
# Generate seeders for all tables
php artisan get:seeder

# Or for specific critical tables
php artisan get:seeder users
php artisan get:seeder products
php artisan get:seeder orders
```

### Working with Multiple Databases

[](#working-with-multiple-databases)

```
# Generate migrations from production database
php artisan get:migration --connection=production

# Generate seeders from staging database
php artisan get:seeder --connection=staging
```

Generated Files
---------------

[](#generated-files)

### Migration Example

[](#migration-example)

```
