如何使用Laravel创建mysql数据库 [英] How to create a mysql db with Laravel

查看:30
本文介绍了如何使用Laravel创建mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.2.我已经设置了我的第一个迁移,我想运行它们.在视频教程中,它没有说明如何创建mysql数据库我知道我可以在phpmyadmin中手动执行此操作,但是有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?

这将安装迁移表:

php artisan migrate:install

是否有类似的命令可以创建数据库?

Is there a similar command that will create the DB?

我认为该过程应该是:

php artisan DB:install (or similar command)

安装迁移表:

php artisan migrate:install

运行迁移:

php artisan migrate

并回滚迁移:

php artisan migrate:rollback

推荐答案

没有提供开箱即用的东西,但是您可以创建自己的命令来为您完成此操作:

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

然后在 app/Console/Commands 中找到 CreateDatabase.php .打开吸盘,让我们进行一些更改:

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'],
    ];
}

然后,我们将创建另一个名为 fire()的函数,该函数将在调用命令时被调用:

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

现在,您将获得一个基于连接配置为您创建的 newdb 数据库.

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

编辑:忘记了最重要的部分-您需要告知 app \ Console \ Commands \ Kernel.php 有关新命令的信息,请确保将其添加到受保护的$ commands [] 数组.

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
];

这篇关于如何使用Laravel创建mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆