如何使用 Laravel 创建一个 mysql 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38832166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a mysql db with Laravel
提问by user1532669
I'm using Laravel 5.2. I've setup my first migrations and I want to run them. From the video tutorialit 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?
我正在使用 Laravel 5.2。我已经设置了我的第一次迁移,我想运行它们。从视频教程中它没有解释如何创建一个 mysql 数据库。我知道我可以在 phpmyadmin 中手动执行此操作,但是有 Laravel 方法吗?
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
回答by Ohgodwhy
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:
然后在app/Console/Commands
你会发现CreateDatabase.php
. 打开那个吸盘,让我们做一些改变:
protected $name = "make:database";
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:
然后我们将调用另一个函数fire()
,该函数将在调用命令时调用:
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.
现在,您将newdb
根据您的连接配置为您创建一个数据库。
EditForgot 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.
编辑忘记了最重要的部分——你需要告诉app\Console\Commands\Kernel.php
你的新命令,确保将它添加到protected $commands[]
数组中。
protected $commands = [
///...,
App\Console\Commands\CreateDatabase::class
];
回答by Vikash
This answer might be useful if you are using different mysql connection also. I am writing code in laravel 5.5
如果您也使用不同的 mysql 连接,这个答案可能很有用。我正在用 Laravel 5.5 编写代码
Step:1Create command
步骤:1创建命令
php artisan make:command CreateDatabaseCommand
Step:2In app/Console/Kernel.php register the command
步骤:2在 app/Console/Kernel.php 中注册命令
protected $commands = [
CreateDatabaseCommand::class
];
Step:3Write logic in your CreateDatabaseCommand.php file
步骤:3在你的 CreateDatabaseCommand.php 文件中编写逻辑
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
注意 ::仅当您想在与默认 mysql 连接不同的连接中创建数据库时才应使用第二个参数,否则命令将自动采用默认的 db 连接
Hope this will help someone :)
希望这会帮助某人:)
回答by Connor Leech
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.
如果您不打算使用 Vagrant box 或虚拟机进行本地开发,那么您将不得不安装您选择的数据库驱动程序,然后创建一个新数据库。
To do that with MySQL from the command line run:
要从命令行使用 MySQL 执行此操作,请运行:
$ mysql -uroot -p
mysql> create database yourDatabaseName;
Then cp .env.example .env
and update your database creds.
然后cp .env.example .env
更新您的数据库凭据。
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:clearbut
php artisan migrate` should work. Below are some tutorial resources you may find helpful:
首次安装驱动程序时,您已经为数据库设置了用户名和密码。在此之后,您可能必须php artisan key:generate
和/或“php artisan config:clear but
php artisan migrate”应该可以工作。以下是一些您可能会觉得有用的教程资源:
回答by Juan Castro Lurita
Vikash's response worked. (in Laravel 5.8)
维卡什的回应奏效了。(在 Laravel 5.8 中)
Based on the response of Vikash: How to create a mysql db with Laravel
基于 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
我创建了以下命令,该命令使用您分配的排序规则创建 MySQL 数据库。我已将其上传到此GitHub 存储库
I hope it helps other developers.
我希望它可以帮助其他开发人员。
回答by Vivek Titwal
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 make:model -m 命令来制作模型和迁移表。之后,在任何文本编辑器中打开迁移表以在数据库表中添加列,然后使用此命令进行迁移:
php artisan migrate
php工匠迁移
回答by Jeremias Araujo
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.
您应该创建数据库,并为其设置连接参数。然后,Laravel 将访问数据库并运行迁移(制作表格)和播种机。
You can found the parameters for php artisan migrate
here: https://laravel.com/docs/5.2/migrations#running-migrations
您可以在php artisan migrate
此处找到参数:https: //laravel.com/docs/5.2/migrations#running-migrations