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

php artisan make:migration create_art_table

这样就会在 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

可以看到创建了4张表
