PHP code example of sirval / laravel-smart-migrations

1. Go to this page and download the library: Download sirval/laravel-smart-migrations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sirval / laravel-smart-migrations example snippets


return [
    'model_namespace' => 'App\\Models',
    'rollback' => true,
    'audit_log_enabled' => false,
    'audit_log_table' => 'smart_migrations_audits',
];

return [
    // Default model namespace for model resolution
    'model_namespace' => 'App\\Models',
    
    // Require user confirmation before rollback
    'rollback' => true,
    
    // Enable audit logging for rollbacks
    'audit_log_enabled' => false,
    
    // Table name for audit logs
    'audit_log_table' => 'smart_migrations_audits',
];

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');

$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);
bash
php artisan vendor:publish --tag="laravel-smart-migrations-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-smart-migrations-config"
bash
php artisan vendor:publish --tag="laravel-smart-migrations-views"
bash
# 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
bash
php artisan migrate:list-model-migrations User