php - How to Create new database and user only for the created database in mysql using Laravel 7?

I want to create a new database and user **only **for created new database And save it on current database.

Ex -

New User - Sam
Password - samDb123 (automatically generating)
database name- samDB (automatically generating)

current database :-

UserName database Name password
sam samDB samDb123
max maxDB pws123

Answer

Solution:

This code should work.

create the database:

DB::getConnection()->statement('CREATE DATABASE :schema', array('schema' => 'sam'));

create db user:

DB::getConnection()->statement('CREATE USER :user@'localhost' IDENTIFIED BY :password; array('user' => 'samDB', 'password' => 'samDb123'));

Source