Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a mysql db with Laravel

Tags:

laravel

I'm using Laravel 5.2. I've setup my first migrations and I want to run them. From the video tutorial it doesn't explain how to create a mysql db. I know I can do this manually in phpmyadmin but is there a Laravel way to do it?

This will install the migrations table:

php artisan migrate:install

Is there a similar command that will create the DB?

I'm thinking the process should be:

php artisan DB:install (or similar command)

Install the migrations table:

php artisan migrate:install

Run the migrations:

php artisan migrate

and to rollback the migrations:

php artisan migrate:rollback
like image 492
user1532669 Avatar asked Aug 08 '16 14:08

user1532669


People also ask

Does Laravel use MySQL?

Currently Laravel supports four database systems: MySQL, Postgres, SQLite, and SQL Server.

How do you create a new database in MySQL?

Right-click the connection name and select New Database. Alternatively, go to the Database menu in the main toolbar and click New Database. 4. In the New Database tab that will open, enter the name for your new database, select charset and collation.

Which DB to use with Laravel?

Laravel makes interacting with databases extremely simple across a variety of supported databases using raw SQL, a fluent query builder, and the Eloquent ORM. Currently, Laravel provides first-party support for five databases: MariaDB 10.3+ (Version Policy) MySQL 5.7+ (Version Policy)


2 Answers

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
];
like image 99
Ohgodwhy Avatar answered Sep 19 '22 02:09

Ohgodwhy


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 :)

like image 36
Vikash Avatar answered Sep 23 '22 02:09

Vikash