How to create new empty database with Laravel over Model

前端 未结 5 1556
清酒与你
清酒与你 2021-01-25 20:55

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

相关标签:
5条回答
  • 2021-01-25 21:02

    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

    0 讨论(0)
  • 2021-01-25 21:11

    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.

    0 讨论(0)
  • 2021-01-25 21:16

    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');
    
    0 讨论(0)
  • 2021-01-25 21:20

    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.

    0 讨论(0)
  • 2021-01-25 21:22

    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

    0 讨论(0)
提交回复
热议问题