PHPackages                             sepehr-mohseni/elosql - 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. sepehr-mohseni/elosql

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

sepehr-mohseni/elosql
=====================

Intelligent Laravel schema analysis, migration generation, and Eloquent model scaffolding from existing databases

v1.0.0(4mo ago)6192↓46.7%MITPHPPHP ^8.1CI passing

Since Jan 6Pushed 4mo agoCompare

[ Source](https://github.com/sepehr-mohseni/elosql)[ Packagist](https://packagist.org/packages/sepehr-mohseni/elosql)[ RSS](/packages/sepehr-mohseni-elosql/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (9)Versions (2)Used By (0)

Elosql
======

[](#elosql)

[![Tests](https://github.com/sepehr-mohseni/elosql/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/sepehr-mohseni/elosql/actions/workflows/tests.yml)[![Latest Stable Version](https://camo.githubusercontent.com/2acf9baae207d854b2d5f08decb51e487bcf5873664c63504d4a58da7f78d39c/68747470733a2f2f706f7365722e707567782e6f72672f7365706568722d6d6f6873656e692f656c6f73716c2f762f737461626c65)](https://packagist.org/packages/sepehr-mohseni/elosql)[![Total Downloads](https://camo.githubusercontent.com/1c710035a866a96e79df6be4d1c0301c881213e92efdb43d564194fd0be35e35/68747470733a2f2f706f7365722e707567782e6f72672f7365706568722d6d6f6873656e692f656c6f73716c2f646f776e6c6f616473)](https://packagist.org/packages/sepehr-mohseni/elosql)[![License](https://camo.githubusercontent.com/2ca78a7bc764a860c01dcc47c37a851b909ca9755145d77add03c93b34042def/68747470733a2f2f706f7365722e707567782e6f72672f7365706568722d6d6f6873656e692f656c6f73716c2f6c6963656e7365)](https://packagist.org/packages/sepehr-mohseni/elosql)[![PHP Version](https://camo.githubusercontent.com/05394fe51fe29368c1c062c62746520b0fadd8228e07fde2ff5a69cf6aa5cc43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7365706568722d6d6f6873656e692f656c6f73716c)](https://packagist.org/packages/sepehr-mohseni/elosql)

**Elosql** is a production-grade Laravel package that intelligently analyzes existing database schemas and generates precise migrations and Eloquent models. It supports MySQL, PostgreSQL, SQLite, and SQL Server, making it perfect for legacy database integration, reverse engineering, and rapid application scaffolding.

Features
--------

[](#features)

- 🔍 **Smart Schema Analysis** - Automatically detects columns, indexes, foreign keys, and table relationships
- 🚀 **Multi-Database Support** - Works with MySQL/MariaDB, PostgreSQL, SQLite, and SQL Server
- 📁 **Migration Generation** - Creates Laravel migrations with proper dependency ordering
- 🏗️ **Model Scaffolding** - Generates Eloquent models with relationships, casts, and fillable attributes
- 🔗 **Relationship Detection** - Automatically detects `belongsTo`, `hasMany`, `hasOne`, `belongsToMany`, and polymorphic relationships
- 📊 **Schema Diff** - Compare database schema with existing migrations
- ⚙️ **Highly Configurable** - Customize every aspect of generation through config or command options
- ✅ **Production Ready** - Comprehensive test suite with 90%+ coverage

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

[](#requirements)

- PHP 8.1 or higher
- Laravel 10.0 or 11.0

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

[](#installation)

Install via Composer:

```
composer require sepehr-mohseni/elosql
```

The package will auto-register its service provider. Optionally, publish the configuration file:

```
php artisan vendor:publish --tag=elosql-config
```

Quick Start
-----------

[](#quick-start)

### Generate Everything

[](#generate-everything)

Generate migrations and models for your entire database:

```
php artisan elosql:schema
```

### Preview Schema

[](#preview-schema)

See what will be generated without creating any files:

```
php artisan elosql:preview
```

### Generate Migrations Only

[](#generate-migrations-only)

```
php artisan elosql:migrations
```

### Generate Models Only

[](#generate-models-only)

```
php artisan elosql:models
```

Commands
--------

[](#commands)

### `elosql:schema`

[](#elosqlschema)

The main command that generates both migrations and models.

```
php artisan elosql:schema [options]

Options:
  --connection=       Database connection to use (default: default connection)
  --table=            Generate for specific table(s), comma-separated
  --exclude=          Exclude specific table(s), comma-separated
  --migrations-path=  Custom path for migrations (default: database/migrations)
  --models-path=      Custom path for models (default: app/Models)
  --models-namespace= Custom namespace for models (default: App\Models)
  --no-migrations     Skip migration generation
  --no-models         Skip model generation
  --force             Overwrite existing files
```

**Examples:**

```
# Generate for specific tables
php artisan elosql:schema --table=users,posts,comments

# Exclude certain tables
php artisan elosql:schema --exclude=migrations,cache,sessions

# Custom output paths
php artisan elosql:schema --migrations-path=database/generated --models-path=app/Domain/Models

# Use a different database connection
php artisan elosql:schema --connection=legacy_db
```

### `elosql:migrations`

[](#elosqlmigrations)

Generate migration files from database schema.

```
php artisan elosql:migrations [options]

Options:
  --connection=   Database connection to use
  --table=        Generate for specific table(s)
  --exclude=      Exclude specific table(s)
  --path=         Custom output path
  --fresh         Generate fresh migrations (ignore existing)
  --diff          Only generate migrations for schema differences
  --force         Overwrite existing files
```

**Examples:**

```
# Generate migrations for a legacy database
php artisan elosql:migrations --connection=legacy --path=database/legacy-migrations

# Generate only new/changed tables
php artisan elosql:migrations --diff
```

### `elosql:models`

[](#elosqlmodels)

Generate Eloquent model files.

```
php artisan elosql:models [options]

Options:
  --connection=   Database connection to use
  --table=        Generate for specific table(s)
  --exclude=      Exclude specific table(s)
  --path=         Custom output path
  --namespace=    Custom namespace
  --preview       Preview generated code without writing files
  --force         Overwrite existing files
```

**Examples:**

```
# Preview model generation
php artisan elosql:models --preview --table=users

# Generate with custom namespace
php artisan elosql:models --namespace="Domain\\User\\Models"
```

### `elosql:preview`

[](#elosqlpreview)

Preview the schema analysis without generating any files.

```
php artisan elosql:preview [options]

Options:
  --connection=   Database connection to use
  --table=        Preview specific table(s)
  --format=       Output format: table, json, yaml (default: table)
```

**Examples:**

```
# JSON output for processing
php artisan elosql:preview --format=json > schema.json

# View specific table structure
php artisan elosql:preview --table=users
```

### `elosql:diff`

[](#elosqldiff)

Show differences between database schema and existing migrations.

```
php artisan elosql:diff [options]

Options:
  --connection=   Database connection to use
  --format=       Output format: table, json (default: table)
```

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

[](#configuration)

After publishing the config file (`config/elosql.php`), you can customize:

### Database Connection

[](#database-connection)

```
'connection' => env('ELOSQL_CONNECTION', null), // null = default connection
```

### Table Filtering

[](#table-filtering)

```
'exclude_tables' => [
    'migrations',
    'failed_jobs',
    'password_resets',
    'personal_access_tokens',
    'cache',
    'sessions',
],
```

### Migration Settings

[](#migration-settings)

```
'migrations' => [
    'path' => database_path('migrations'),
    'separate_foreign_keys' => true, // Generate FK migrations separately
    'include_drop_tables' => true,   // Include down() method
],
```

### Model Settings

[](#model-settings)

```
'models' => [
    'path' => app_path('Models'),
    'namespace' => 'App\\Models',
    'base_class' => \Illuminate\Database\Eloquent\Model::class,
    'use_guarded' => false,           // Use $guarded instead of $fillable
    'generate_phpdoc' => true,        // Generate PHPDoc blocks
    'detect_soft_deletes' => true,    // Auto-detect SoftDeletes trait
    'detect_timestamps' => true,      // Auto-detect timestamp columns
],
```

### Type Mappings

[](#type-mappings)

Customize how database types map to Laravel migration methods:

```
'type_mappings' => [
    'mysql' => [
        'tinyint(1)' => 'boolean',
        'json' => 'json',
        // Add custom mappings
    ],
    'pgsql' => [
        'jsonb' => 'jsonb',
        'uuid' => 'uuid',
    ],
],
```

### Relationship Detection

[](#relationship-detection)

```
'relationships' => [
    'detect_belongs_to' => true,
    'detect_has_many' => true,
    'detect_has_one' => true,
    'detect_belongs_to_many' => true,
    'detect_morph' => true,
    'pivot_table_patterns' => [
        // Regex patterns for detecting pivot tables
        '/^([a-z]+)_([a-z]+)$/',
    ],
],
```

Generated Code Examples
-----------------------

[](#generated-code-examples)

### Migration Example

[](#migration-example)

```
