PHPackages                             sirval/laravel-smart-migrations - 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. sirval/laravel-smart-migrations

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

sirval/laravel-smart-migrations
===============================

Intelligently manage Laravel migrations with safe rollback by table or model, foreign key detection, preview mode, and comprehensive migration analysis tools

v1.0.0(6mo ago)457[2 PRs](https://github.com/sirval/laravel-smart-migrations/pulls)MITPHPPHP ^8.1CI passing

Since Dec 28Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/sirval/laravel-smart-migrations)[ Packagist](https://packagist.org/packages/sirval/laravel-smart-migrations)[ Docs](https://github.com/sirval/laravel-smart-migrations)[ GitHub Sponsors](https://github.com/sirval)[ RSS](/packages/sirval-laravel-smart-migrations/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (13)Versions (6)Used By (0)

Laravel Smart Migrations
========================

[](#laravel-smart-migrations)

Intelligently manage Laravel migrations with safe rollback, analysis, validation, and comprehensive tools for table and model-based operations.

[![Latest Version on Packagist](https://camo.githubusercontent.com/d0daba78ed49337a474cb77fa931bd627cb1e46702c793d093030c314b991585/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73697276616c2f6c61726176656c2d736d6172742d6d6967726174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sirval/laravel-smart-migrations)[![GitHub Tests Action Status](https://camo.githubusercontent.com/77595174ac7b0f0ce92aeaddafda0670fbeff5bfac6bd0555343925496fff10f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73697276616c2f6c61726176656c2d736d6172742d6d6967726174696f6e732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/sirval/laravel-smart-migrations/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/abb7f606f8b5a84c14a16e4acc4e6f87b2f5dc47c7863bc8406f60e2825fc081/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73697276616c2f6c61726176656c2d736d6172742d6d6967726174696f6e732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/sirval/laravel-smart-migrations/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/ba0343f3e1c4a4e4a12ad373e4d206179dd5b2fedf4af61f1932d7af519b6af3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73697276616c2f6c61726176656c2d736d6172742d6d6967726174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sirval/laravel-smart-migrations)

A comprehensive Laravel package for intelligent migration management. Roll back migrations by table name or model, with explicit, safe options to prevent accidental data loss. Future versions will include analysis, validation, and optimization tools.

⭐ Give us a Star
----------------

[](#-give-us-a-star)

If you find this package helpful, please consider giving us a star on GitHub! It helps us continue maintaining and improving this package. Your support means a lot to us! ☕

[⭐ Star us on GitHub](https://github.com/sirval/laravel-smart-migrations)

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

[](#installation)

You can install the package via composer:

```
composer require sirval/laravel-smart-migrations
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="laravel-smart-migrations-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-smart-migrations-config"
```

This is the contents of the published config file:

```
return [
    'model_namespace' => 'App\\Models',
    'require_confirmation' => true,
    'show_details' => true,
    'prevent_multi_batch_rollback' => true,
    'audit_log_enabled' => false,
    'audit_log_table' => 'smart_migrations_audits',
];
```

Optionally, you can publish the views using

```
php artisan vendor:publish --tag="laravel-smart-migrations-views"
```

Usage
-----

[](#usage)

### Available Commands

[](#available-commands)

#### 1. Rollback by Table Name

[](#1-rollback-by-table-name)

Roll back all migrations for a specific table:

```
# Rollback latest migration only
php artisan migrate:rollback-table users --latest
php artisan migrate:rollback-table users -L

# Rollback oldest migration only
php artisan migrate:rollback-table users --oldest
php artisan migrate:rollback-table users -O

# Rollback all migrations for this table
php artisan migrate:rollback-table users --all
php artisan migrate:rollback-table users -A

# Rollback migrations from specific batch
php artisan migrate:rollback-table users --batch=5
php artisan migrate:rollback-table users -B 5

# Skip confirmation prompt
php artisan migrate:rollback-table users --force
php artisan migrate:rollback-table users -F

# Interactive mode (choose from available options)
php artisan migrate:rollback-table users --interactive
php artisan migrate:rollback-table users -I

# Preview the changes without executing
php artisan migrate:rollback-table users --preview

# Check for foreign key constraints
php artisan migrate:rollback-table users --check-fk

# Combine options: preview with foreign key check
php artisan migrate:rollback-table users --preview --check-fk

# Rollback multiple tables at once
php artisan migrate:rollback-table users,posts,comments --latest
php artisan migrate:rollback-table users,posts,comments -L
php artisan migrate:rollback-table users posts comments --oldest
php artisan migrate:rollback-table users posts comments -O
```

#### 2. Rollback by Model Name

[](#2-rollback-by-model-name)

Roll back all migrations associated with a specific model:

```
# Rollback latest migration
php artisan migrate:rollback-model User --latest
php artisan migrate:rollback-model User -L

# Rollback oldest migration
php artisan migrate:rollback-model User --oldest
php artisan migrate:rollback-model User -O

# Rollback all migrations
php artisan migrate:rollback-model User --all
php artisan migrate:rollback-model User -A

# Rollback by batch
php artisan migrate:rollback-model User --batch=5
php artisan migrate:rollback-model User -B 5

# Skip confirmation
php artisan migrate:rollback-model User --force
php artisan migrate:rollback-model User -F

# Interactive mode
php artisan migrate:rollback-model User --interactive
php artisan migrate:rollback-model User -I

# Preview the changes without executing
php artisan migrate:rollback-model User --preview

# Check for foreign key constraints
php artisan migrate:rollback-model User --check-fk

# Combine options: preview with foreign key check
php artisan migrate:rollback-model User --preview --check-fk

# Rollback multiple models at once
php artisan migrate:rollback-model User,Post,Comment --latest
php artisan migrate:rollback-model User,Post,Comment -L
php artisan migrate:rollback-model User Post Comment --oldest
php artisan migrate:rollback-model User Post Comment -O
```

#### 3. Rollback by Batch Number

[](#3-rollback-by-batch-number)

Roll back all migrations from a specific batch:

```
# Rollback all migrations from batch 5
php artisan migrate:rollback-batch 5

# Skip confirmation
php artisan migrate:rollback-batch 5 --force
php artisan migrate:rollback-batch 5 -F
```

#### 4. List Migrations for Table

[](#4-list-migrations-for-table)

View all migrations affecting a specific table:

```
php artisan migrate:list-table-migrations users
```

#### 5. List Migrations for Model

[](#5-list-migrations-for-model)

View all migrations associated with a specific model:

```
php artisan migrate:list-model-migrations User
```

### Command Options

[](#command-options)

OptionShortDescriptionDefault`--latest``-L`Only rollback the latest migration`false``--oldest``-O`Only rollback the oldest migration`false``--all``-A`Rollback all migrations (ignore batch restrictions)`false``--batch=N``-B`Only rollback migrations from batch N`null``--force``-F`Skip confirmation prompts`false``--interactive``-I`Show options and let user choose`false``--preview`—Preview changes without executing rollback`false``--check-fk`—Check and display foreign key constraints`false`### Advanced Features

[](#advanced-features)

#### Preview Mode (`--preview`)

[](#preview-mode---preview)

Preview what migrations will be rolled back without making any database changes. This is useful for testing and validation before executing the actual rollback.

```
# Preview rollback for a table
php artisan migrate:rollback-table users --preview

# Preview with other options
php artisan migrate:rollback-table users --latest --preview

# Preview multiple tables
php artisan migrate:rollback-table users,posts --preview
```

When you run with `--preview`, the command will:

- Show all migrations that would be rolled back
- Display any foreign key constraints (if `--check-fk` is used)
- Return successfully without making any changes to the database

#### Foreign Key Detection (`--check-fk`)

[](#foreign-key-detection---check-fk)

Automatically detect and display all foreign key constraints before rolling back migrations. This helps prevent breaking references between tables.

```
# Check foreign keys for a table
php artisan migrate:rollback-table users --check-fk

# Combine with preview for full safety
php artisan migrate:rollback-table users --check-fk --preview

# Check multiple tables
php artisan migrate:rollback-table users,posts,comments --check-fk
```

The foreign key check displays:

- **Foreign Keys Defined in the Table**: Keys that reference other tables
- **Dependent Foreign Keys**: Other tables that reference this table

Supported Databases:

- **MySQL**: Uses `INFORMATION_SCHEMA` queries
- **PostgreSQL**: Uses `information_schema` views
- **SQLite**: Uses `PRAGMA foreign_key_list()`

#### Combined Usage

[](#combined-usage)

For maximum safety, combine preview mode with foreign key detection:

```
# Most cautious approach
php artisan migrate:rollback-table users --preview --check-fk

# Works with all other options
php artisan migrate:rollback-table users --latest --preview --check-fk --force

# Multiple tables with full checks
php artisan migrate:rollback-table users,posts,comments --all --preview --check-fk
```

This allows you to:

1. See all migrations that will be rolled back
2. Identify any foreign key constraints
3. Verify safety without making changes
4. Execute with confidence when ready

### Configuration

[](#configuration)

The package respects these configuration options from `config/smart-migrations.php`:

```
return [
    // Default model namespace for model resolution
    'model_namespace' => 'App\\Models',

    // Require user confirmation before rollback
    'require_confirmation' => true,

    // Show detailed migration information
    'show_details' => true,

    // Prevent rolling back multiple batches at once
    'prevent_multi_batch_rollback' => true,

    // Enable audit logging for rollbacks
    'audit_log_enabled' => false,

    // Table name for audit logs
    'audit_log_table' => 'smart_migrations_audits',
];
```

### Programmatic Usage

[](#programmatic-usage)

Use the SmartMigrations facade for programmatic access:

```
use Sirval\LaravelSmartMigrations\Facades\SmartMigrations;

// Rollback by table
$results = SmartMigrations::rollbackTable('users', ['latest' => true]);

// Rollback by model
$results = SmartMigrations::rollbackModel('User', ['latest' => true]);

// Rollback by batch
$results = SmartMigrations::rollbackBatch(5);

// List migrations for table
$migrations = SmartMigrations::listMigrationsForTable('users');

// List migrations for model
$migrations = SmartMigrations::listMigrationsForModel('User');

// Get table status
$status = SmartMigrations::getTableStatus('users');

// Get model status
$status = SmartMigrations::getModelStatus('User');
```

### Available Options for Rollback Methods

[](#available-options-for-rollback-methods)

When calling rollback methods programmatically, pass options as an array:

```
$options = [
    'latest' => true,      // Rollback latest only
    'oldest' => false,     // Rollback oldest only
    'all' => false,        // Rollback all
    'batch' => null,       // Specific batch number
    'force' => false,      // Skip confirmation
    'dry_run' => false,    // Preview without executing
];

SmartMigrations::rollbackTable('users', $options);
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Ohuka Ikenna](https://github.com/sirval)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance78

Regular maintenance activity

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

189d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/20794595?v=4)[Ohuka Ikenna Val](/maintainers/sirval)[@sirval](https://github.com/sirval)

---

Top Contributors

[![sirval](https://avatars.githubusercontent.com/u/20794595?v=4)](https://github.com/sirval "sirval (35 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laraveldatabasemigrationsrollbackforeign-keysmigration-managementtable-rollbackmodel-rollbackbatch-rollbackpreview-modemigration-tools

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/sirval-laravel-smart-migrations/health.svg)

```
[![Health](https://phpackages.com/badges/sirval-laravel-smart-migrations/health.svg)](https://phpackages.com/packages/sirval-laravel-smart-migrations)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k54.9M11.7k](/packages/illuminate-database)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

213421.4k2](/packages/wnx-laravel-backup-restore)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17558.6k](/packages/lacodix-laravel-model-filter)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

44855.7k](/packages/harris21-laravel-fuse)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
