How to create new empty database with Laravel over Model

こ雲淡風輕ζ 提交于 2020-01-21 19:17:34

问题


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:


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.




回答2:


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



回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/36914573/how-to-create-new-empty-database-with-laravel-over-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!