Building Dynamic Databases with Laravel: Step-by-Step

How to create Database dynamically in Laravel

Building-Dynamic-Databases-with-Laravel-Step-by-Step

In this article we will see how can you create Database dynamically with code in Laravel . Laravel provide lots of solution to your DB related problem as we can use its built in methods as well as raw PHP in our code it opens a lots of way to solve a problem in Laravel .

Here we will see to create database dynamically but you can also perform the following actions also dynamically with laravel .

  • Run migrations dynamically in Laravel
  • Import SQL file to a table in Laravel dynamically

OK , let's see how can we create Database dynamically .

Create you route :-

First create a route to redirect to your controller .

Route::get('createdb', [HomeController::class,'createDB']);

Code on Controller :-

Now understand the following code written on controller and implement as you need for your application .

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function createDB()
    {
        try{
            $new_db_name = "DB_".rand()."_".time();
            $new_mysql_username = "root";
            $new_mysql_password = "";

            $conn = mysqli_connect(
                config('database.connections.mysql.host'), 
                env('DB_USERNAME'), 
                env('DB_PASSWORD')
            );
            if(!$conn ) {
                return false;
            }
            $sql = 'CREATE Database IF NOT EXISTS '.$new_db_name;
            $exec_query = mysqli_query( $conn, $sql);
            if(!$exec_query) {
                die('Could not create database: ' . mysqli_error($conn));
            }
            return 'Database created successfully with name '.$new_db_name;
        }
        catch(\Exception $e){
            return false;
        }
    }
}


Output :-

Now run this API as you need , for me i am running this API on my postman and this is the response and created Database on PhpMyAdmin .

Create Database dynamically in Laravel

Create Database dynamically in Laravel

4 Comments

  1. thank you for this code!!!
    is there is any way to create database in the main database. mean to say

    that there will be a form where a new user will registered their account .
    and after email verification when user verify their email , then for that specific user a new database for him will be create dynamically.

    i want this in laravel, so plzz if you can help it will be awesome...!!!

    ReplyDelete
  2. all new database of the registered users will be the sub-database of the main database....!!!

    ReplyDelete
    Replies
    1. Hi , thank you for connecting. as per your requirements i can suggest some following things.

      -> As you know there is no concept of sub-databases we have to create multiple databases.
      -> yes ,you can follow such a way that , you will maintain a master Database whenever user registers you can store it on master database .
      -> After successfull verification you can make a new database and copy the user's detail from master database to new database.

      Delete
  3. 👍👍👍👍👍👍

    ReplyDelete
Previous Post Next Post

Contact Form