Smoothly Transition Data with Laravel's Migration from Code

 How to run migration dynamically in Laravel

Smoothly-Transition-Data-with-Laravel's-Migration-from-Code

In this article we will learn how to run migration from code in Laravel . Whenever we are working on application we basically run php artisan migrate on our console to migration our tables on Database , but there are lots of time we got requirements where we have to create tables dynamically in Laravel we don't have to run migrate command again and again on server's console to run migration . We can simply do it from our code .

Here we will take two cases into account as below .

  • Run migration on connected database.
  • Connect to dynamic database and run migration.

Before migration :-

Run migration from code in Laravel

Run migration on connected Database :-

It's very simple to run migration on connected Database we just have to provide a built-in method Artisan::call('migration') to run migration as show below .

HomeController.php :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Artisan;
use DB;

class HomeController extends Controller
{
    public function runMigration()
    {
        try{
            // RUN MIGRATION ON CONNECTED DB
            Artisan::call('migrate');
            // CLEAR CACHE OF YOUR APP
            Artisan::call('optimize:clear');
            Artisan::call('config:cache');
            return 'Migration ran successfully';
        }
        catch(\Exception $e)
        {
            return false;
        }
    }
}

Connect to Dynamic database and run migration :

Here in this case we will try connect dynamic database according to your requirement . Here basically what i am doing is , i have setup my different database connection on config/database.php so that i can connect to different databases , here i have hardcoded the database connection , make sure you make dynamic as per your requirement .

config/database.php :

'connections' => [
        // database = devtrigger
       'db_b3JnYW5pemF0aW9uLWRiLTI5Ng' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => '127.0.0.1',
        'port' => env('DB_PORT', '3306'),
        'database' => 'devtrigger',
        'username' => 'root',
        'password' => '',
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => false,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],
]

HomeController.php :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Artisan;
use DB;

class HomeController extends Controller
{
    public function runMigration()
    {
        try{
                // GET DATABASE CONNECTION KEY FROM DATABASE.PHP FILE
                $db_name = "db_b3JnYW5pemF0aW9uLWRiLTI5Ng";
                // CONNECTING TO DATABASE
                Config::set('database.default.'.$db_name); 
                DB::purge($db_name);
                Artisan::call('migrate', ['--database' => $db_name]);
                return 'Migration done on '.$db_name;
        }
        catch(\Exception $e)
        {
            return false;
        }
    }
}

In this way you can run migration on your connected database as well as dynamic databases .

After Migration :-

Run migration from code in Laravel



Previous Post Next Post

Contact Form