gpt4 book ai didi

database - 如何在 Laravel 中创建数据库模式(表)?

转载 作者:太空狗 更新时间:2023-10-30 01:53:00 25 4
gpt4 key购买 nike

我是 Laravel 的新手。我正在尝试在 Laravel 中创建数据库。我在控制台中尝试过:

Schema::create

但它给出“找不到命令”。我应该安装什么或如何创建数据库?

最佳答案

我的工作示例:

创建新工匠命令:

php artisan make:command mysql

App\Console\Commands\mysql.php 的内容:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class mysql extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mysql:createdb {name?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new mysql database schema based on the database config file';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$schemaName = $this->argument('name') ?: config("database.connections.mysql.database");
$charset = config("database.connections.mysql.charset",'utf8mb4');
$collation = config("database.connections.mysql.collation",'utf8mb4_unicode_ci');

config(["database.connections.mysql.database" => null]);

$query = "CREATE DATABASE IF NOT EXISTS $schemaName CHARACTER SET $charset COLLATE $collation;";

DB::statement($query);

config(["database.connections.mysql.database" => $schemaName]);

}
}

然后运行:(schema_name 是可选的)

php artisan mysql:createdb schema_name

关于database - 如何在 Laravel 中创建数据库模式(表)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32191135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com