Laravel5.4 使用 migrate 创建数据表

2019-09-01   阅读:2005   分类:后端    标签: Laravel

Laravel有个建议,建议文章表名以名词+复数的形式,外键以名词+下划+id的形式。

创建表名:arts

外键:art_id

时间:created_at/updated_at

Laravel 提供了 migration,数据库迁移。


Usage:

make:migration [options] [--] <name>

Arguments:

name The name of the migration.

Options:

--create[=CREATE] The table to be created.

--table[=TABLE] The table to migrate.

--path[=PATH] The location where the migration file should be created.

-h, --help Display this help message

-q, --quiet Do not output any message

-V, --version Display this application version

--ansi Force ANSI output

--no-ansi Disable ANSI output

-n, --no-interaction Do not ask any interactive question

--env[=ENV] The environment the command should run under

-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:

Create a new migration file

创建表:

laravel 默认已经有两个migration

image.png

php artisan make:migration create_art_table

image.png

这样就会在 database\migrations下创建 2019_08_31_173421_create_arts_table.php文件,我们打开

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
class CreateArtsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() //migration要执行的行数
    {
        
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() //migration要执行的行数
    {
       
    }
}

执行: 使用migrations的

php artisan migrate

回滚使用的是:

migrate:rollback

创建表:修改文件并保存

<?php

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

class CreateArtsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('arts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title',100)->default("");
            $table->text('content');
            $table->integer('arts_id')->default("0");
            $table->timestamps();
        });
    }

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

执行命令:

php artisan migrate

image.png

可以看到创建了4张表

image.png

【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/207.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×