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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32191135/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:58:32  来源:igfitidea点击:

How to create database schema (table) in Laravel?

databaselaravel

提问by ci_lover

I'm new to Laravel.I'm trying to create database in Laravel. I tried in console with:

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

Schema::create

But it's giving 'command not found'. What should I install or how to create database?

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

采纳答案by ci_lover

First you have to set database name,username and password in database.php in config folder.it look like

首先你必须在配置文件夹中的database.php中设置数据库名称,用户名和密码。它看起来像

'connections' => [

        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => storage_path('database.sqlite'),
            'prefix'   => '',
        ],

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'news'),
            'username'  => env('DB_USERNAME', 'root'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'pgsql' => [
            'driver'   => 'pgsql',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
        ],

        'sqlsrv' => [
            'driver'   => 'sqlsrv',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
        ],

    ],

if you are using xampp then right click on your project folder and click on use composer here

如果您使用的是 xampp,则右键单击您的项目文件夹,然后单击此处使用 composer

then run following command

然后运行以下命令

 php artisan migrate:install

and you can create table like

你可以像这样创建表

php artisan make:migration create_users_table

Migration Structure

迁移结构

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should simply reverse the operations performed by the up method.

一个迁移类包含两个方法:up 和 down。up 方法用于向数据库添加新表、列或索引,而 down 方法应该简单地反转 up 方法执行的操作。

Within both of these methods you may use the Laravel schema builder to expressively create and modify tables. To learn about all of the methods available on the Schema builder, check out its documentation. For example, let's look at a sample migration that creates a flights table:

在这两种方法中,您可以使用 Laravel 模式构建器来表达地创建和修改表。要了解架构构建器上可用的所有方法,请查看其文档。例如,让我们看一个创建航班表的示例迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

To know all artisan command run following command

要了解所有工匠命令,请运行以下命令

php artisan

for more read following document http://laravel.com/docs/5.1/migrations

更多阅读以下文档http://laravel.com/docs/5.1/migrations

回答by Roi Dayan

my working example:

我的工作示例:

create new artisan command:

创建新的工匠命令:

php artisan make:command mysql

the content of App\Console\Commands\mysql.php :

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

    }
}

then run: (schema_name is optionally)

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

php artisan mysql:createdb schema_name

回答by Aman Kumar

Database setup in Laravel

Laravel 中的数据库设置

  • open .envfile on root
  • .env在 root 上打开文件

example :-

例子 :-

  DB_CONNECTION=mysql
  DB_HOST=127.0.0.1
  DB_PORT=3306
  DB_DATABASE=dbname
  DB_USERNAME=root
  DB_PASSWORD=password

Now run command

现在运行命令

  php artisan migrate:install
  php artisan make:migration users // for creating new table 

回答by Shota

If you want to create model with artisan do it this way:

如果你想用 artisan 创建模型,请这样做:

php artisan make:model ModelName

回答by Vladimir Kovic

I just created an interesting Laravel package that has some handy set of artisan commands like create or drop database, dump database or load from .sql dump, or to see which fields (and field types) are present in your models ...

我刚刚创建了一个有趣的 Laravel 包,其中包含一些方便的工匠命令,例如创建或删除数据库、转储数据库或从 .sql 转储加载,或者查看模型中存在哪些字段(和字段类型)......

Check it out: https://github.com/vkovic/laravel-commando

看看:https: //github.com/vkovic/laravel-commando