How to create a mysql db with Laravel

前端 未结 6 801
盖世英雄少女心
盖世英雄少女心 2021-02-13 14:50

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 man

相关标签:
6条回答
  • 2021-02-13 15:08

    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

    0 讨论(0)
  • 2021-02-13 15:08

    you have to make your modal first and make its migration table also in order to make a database. you should use: php artisan make:model -m command to make model and migration table. after that open your migration table in any texteditor to add the columns in your database table and then, use this command to migrate:

    php artisan migrate

    0 讨论(0)
  • 2021-02-13 15:20

    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.

    0 讨论(0)
  • 2021-02-13 15:31

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

    0 讨论(0)
  • 2021-02-13 15:32

    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
    ];
    
    0 讨论(0)
  • 2021-02-13 15:32

    You should create the DB, and set the connection parameters to it. Then, Laravel will access the DB and will run the migrations (make the tables) and the seeders.

    You can found the parameters for php artisan migrate here: https://laravel.com/docs/5.2/migrations#running-migrations

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