如何在Laravel中创建数据库架构(表)? [英] How to create database schema (table) in Laravel?

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

问题描述

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

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

Schema::create

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

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

推荐答案

首先,您必须在config文件夹中的database.php中设置数据库名称,用户名和密码.

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

'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'   => '',
        ],

    ],

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

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

然后运行以下命令

 php artisan migrate:install

您可以创建类似的表

php artisan make:migration create_users_table

迁移结构

迁移类包含两个方法:向上和向下.up方法用于将新表,列或索引添加到数据库中,而down方法应简单地逆转up方法执行的操作.

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.

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

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:

<?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

有关更多信息,请阅读以下文档 http://laravel.com/docs/5.1/migrations

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

这篇关于如何在Laravel中创建数据库架构(表)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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