How to create mysql db using Laravel

I am using Laravel 5.2. I configured my first migrations and I want to run them. From the video tutorial, he does not explain how to create mysql db. I know I can do it manually in phpmyadmin, but is there any way Laravel can do this?

This will install the migration table:

php artisan migrate:install 

Is there a similar command that will create the database?

I think the process should be:

 php artisan DB:install (or similar command) 

Install the migration table:

 php artisan migrate:install 

Run the migration:

 php artisan migrate 

and cancel the migration:

 php artisan migrate:rollback 
+10
source share
6 answers

Nothing is provided, but you can make your own team 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 will find CreateDatabase.php . Open this suction cup and make a few changes:

 protected $name = "make:database"; 

Then below in your file we need a new function:

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

Then we will create another function called fire() , which will be called when the command is called:

 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 will receive the newdb database created for you based on the configuration of your connection.

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

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

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

Step: 1 Create a team

 php artisan make:command CreateDatabaseCommand 

Step: 2 In app / Console / Kernel.php register the command

 protected $commands = [ CreateDatabaseCommand::class ]; 

Step: 3 Write the logic in the 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()); } } 

It's all. Now run the command

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

Note :: . You should use the second argument only if you want to create a database in any other connection from the default mysql connection, otherwise the command will automatically accept the default connection db

Hope this helps someone :)

+5
source

If you are not going to use a firewall or virtual machine for local development, you will need to install your database driver of your choice, and then create a new database.

To do this with MySQL from the command line:

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

Then cp .env.example .env and upgrade your database accounts.

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

When installing the driver for the first time, you must set the username and password in your database. After that, you may need php artisan key:generate and / or 'php artisan config: clear but php artisan migrate` should work. Below are some useful resources that can help you:

Initial database creation and seeding with Laravel 5

How to install MySQL on OSX

+1
source

you need to first make your modal code and make a migration table also to create the database. you should use: php artisan make: model -m command to create the model table and migration. after that, open the migration table in any text editor to add columns to the database table, and then use this command to transfer:

php artisan migrate

0
source

You must create a database and set connection parameters for it. Then Laravel will access the database and will perform the migration (make tables) and seeders.

Here you can find the options for php artisan migrate : https://laravel.com/docs/5.2/migrations#running-migrations

0
source

Wikash's answer worked. (in Laravel 5.8)

Based on Wikash's answer: How to create a mysql database using Laravel

I created the following command, which creates a MySQL database with the mapping you assigned. I uploaded it to this GitHub repository

Hope this helps other developers.

0
source

All Articles