is there any way to create new database with Laravel
CodeBehind ? I mean, I'm not able to use php artisan
or something like that. Just because I want to create a database for every registered user. After that I can use migration
.
I'm using Laravel 5.2
.
Thanks for your support already.

- 1,500
- 7
- 27
- 51
-
well, so, for each user in some table, you wanna create a database for it? – PlayMa256 Apr 28 '16 at 12:40
-
Yes, I have general db for store registered users. But I need to new db for every user. – Yasin Yörük Apr 28 '16 at 12:41
-
This kind of solution is called "multi-tenant", here is an article that describe it [Multi-Tenant Data Architecture](https://msdn.microsoft.com/en-us/library/aa479086.aspx) – UselesssCat May 30 '17 at 20:07
-
@ArieCwHat Thanks for the document. I read that before. But I change my project to as a service model. – Yasin Yörük May 31 '17 at 18:49
5 Answers
You can create as many Databases from controller by passing the dbname like this
public function index() {
$userName = 'bigboytr'; // Your Database name to be created
DB::statement("CREATE DATABASE $userName");
}
Warning :
You don't need to create one database for every user as it will make the system very complex. Ex., If you have some thousands of user in some case, then moving the user's information to registered users 'general' database would be very tough
Recommendation :
What i suggest you it to create a new table for unregistered user, so that you can move them to registered user's table, which would make your process easy.
Tip :
You can compare the Database Engine and choose the best for your match like this

- 1
- 1

- 11,330
- 12
- 48
- 63
for this, you can create an Artisan Command
Step:1 Create command
php artisan make:command CreateDatabaseCommand
Step:2 In app/Console/Kernel.php register the command
protected $commands = [
CreateDatabaseCommand::class
];
Step:3 Write logic in your CreateDatabaseCommand.php file
protected $signature = 'make:database {dbname} {connection?}';
public function handle()
{
try{
$dbname = $this->argument('dbname');
$connection = $this->hasArgument('connection') && $this->argument('connection') ? $this->argument('connection'): DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME);
$hasDb = DB::connection($connection)->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "."'".$dbname."'");
if(empty($hasDb)) {
DB::connection($connection)->select('CREATE DATABASE '. $dbname);
$this->info("Database '$dbname' created for '$connection' connection");
}
else {
$this->info("Database $dbname already exists for $connection connection");
}
}
catch (\Exception $e){
$this->error($e->getMessage());
}
}
Now your ready Artisan command call like this way
php artisan make:database {database-name} {connection-name}
See my sample

- 1
- 17
- 36
well, you could perform a database query for each bd and than use the mysql or you sgbd database create command, for eg;
DB::getConnection()->statement('CREATE DATABASE :schema', array('schema' => "dasdsdasdsadsadsa"))
It probably will work with what you want.

- 6,603
- 2
- 34
- 54
-
Yes it's simple and solved my problem. Thanks to you and everyone to answered or commented my question. – Yasin Yörük Apr 29 '16 at 08:59
-
You can run artisan commands from code.
Artisan::call('migrate');
To run composer commands (you'll need it to register migrations), use php shell_exec
:
shell_exec('composer dumpauto');

- 158,981
- 26
- 290
- 279
You can create a database using DB facade like this (provided the sql user has the rights to do that):
DB::statement('CREATE DATABASE myNewDB');
But I would urge you to reconsider your data structures - having a separate database for every user is most probably not the right solution to whatever problem you are having and could create more issues than it would solve.

- 1,863
- 11
- 15
-
Thanks for your advice and I agree you, it's a bit complicated. But we have to create DB for every user. Actually user is not user. User is a company. So companies has lot of data. I can't keep them in one DB. By the way, it's a question. I'm newbie on laravel. Where I'll run this code. It will controller or model ? – Yasin Yörük Apr 28 '16 at 12:55
-
Thats up to you to decide. Generally you would not want to put too much logic into laravel controllers because code in controllers cannot be reused in a nice way, but if this database creation happens only in one place then you could get away with that. Otherwise I would try to go with object-oriented approach and do database creation as a method in an appropriate entity model, such as $company->createDatabase() – Tadas Paplauskas Apr 28 '16 at 14:01