Laravel 数据迁移创建数据表
1、使用工匠指令创建迁移文件
php artisan make:migration create_article_table
文件位置:D:\server\www\laravel\blog\database\migrations\2020_03_26_030143_create_article_table.php
2、在迁移文件里面创建数据表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateArticleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('article', function (Blueprint $table) {
$table->id();
$table->integer('article_category_id')->default(-1)->comment('文章分类id');
$table->string('title', 220)->comment('标题');
$table->text('content')->comment('内容');
$table->string('author',50)->comment('作者');
$table->smallInteger('hits')->default(0)->comment('点击数');
$table->smallInteger('sort')->default(0)->comment('排序');
$table->smallInteger('recommend')->default(0)->comment('是否推荐 1推荐 0不推荐');
$table->smallInteger('status')->default(0)->comment('状态 1启用 0未启用');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('article');
}
}
3、运行迁移文件
D:\server\www\laravel\blog>php artisan migrate
Migrating: 2020_03_26_030143_create_article_table
Migrated: 2020_03_26_030143_create_article_table (0.2 seconds)
4、查看生成的article数据表
CREATE TABLE `article` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`article_category_id` int(11) NOT NULL DEFAULT '-1' COMMENT '文章分类id',
`title` varchar(220) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '标题',
`content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '内容',
`author` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '作者',
`hits` smallint(6) NOT NULL DEFAULT '0' COMMENT '点击数',
`sort` smallint(6) NOT NULL DEFAULT '0' COMMENT '排序',
`recommend` smallint(6) NOT NULL DEFAULT '0' COMMENT '是否推荐 1推荐 0不推荐',
`status` smallint(6) NOT NULL DEFAULT '0' COMMENT '状态 1启用 0未启用',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
本作品采用《CC 协议》,转载必须注明作者和本文链接