How to create a mysql db with Laravel

This answer might be useful if you are using different mysql connection also. I am writing code in laravel 5.5

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());
     }
   }

That's all. Now run your command

php artisan make:database {your-database-name} {your-connection-name}:

Note :: You should use second argument only if you want to create database in any different connection from default mysql connection otherwise command will automatically take the default db connection

Hope this will help someone :)


If you're not going to use a Vagrant box or virtual machine for local development then you're going to have to install your database driver of choice and then create a new database.

To do that with MySQL from the command line run:

$ mysql -uroot -p
mysql> create database yourDatabaseName;

Then cp .env.example .env and update your database creds.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourDatabaseName
DB_USERNAME=root
DB_PASSWORD=root 

You'll have set up the username and password to your database when you first installed the driver. After this you may have to php artisan key:generate and/or 'php artisan config:clearbutphp artisan migrate` should work. Below are some tutorial resources you may find helpful:

Initial database creation and seeding with Laravel 5

How to install MySQL on OSX


Nothing provided out of the box but you could make your own command that could do this for you:

php artisan make:console CreateDatabase
// Note, in 5.3 this is make:command

Then in app/Console/Commands you'll find CreateDatabase.php. Open that sucker up and let's make a few changes:

protected $name = "make:database";
// in Laravel 5.3 + it's protected $signature

Then down below in your file we need a new function:

protected function getArguments()
{
    return [
        ['name', InputArgument::REQUIRED, 'The name of the database'],
    ];
}

Then we'll make another function called fire() which will be called upon invocation of the command:

public function fire()
{
    DB::getConnection()->statement('CREATE DATABASE :schema', ['schema' => $this->argument('name')]);
} 

And now you can just do this:

php artisan make:database newdb

Now you'll get a newdb database created for you based on your connection configuration.

Edit Forgot the most important part - you need to tell app\Console\Commands\Kernel.php about your new comand, make sure to add it to the protected $commands[] array.

protected $commands = [
    ///...,
    App\Console\Commands\CreateDatabase::class
];

Vikash's response worked. (in Laravel 5.8)

Based on the response of Vikash: How to create a mysql db with Laravel

I have created the following command which creates a MySQL database with the collation that you assign. I have uploaded it to this GitHub repository

I hope it helps other developers.

Tags:

Laravel