5、数据库驱动与建表
Laravel 构建的实在是太友好,不懂数据库也可以进行开发。在前文中我们已经把数据库驱动改成使用 MySQL 了,并执行了迁移文件, 因此,关于更改数据驱动,就不在过多介绍了。
创建分类表迁移文件
我们的文章一般都有分类,现在,我们通过迁移文件来创建数据表吧。
使用 php artisan make:migration
命令可以创建对应数据表的迁移文件。
1、创建迁移文件
$ php artisan make:migration "create categorites table"
INFO Migration [database/migrations/2024_04_04_231218_create_categorites_table.php] created successfully.
该命令会在 database/migrations
目录生成对应的迁移文件。
2、修改迁移文件
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name')->comment('分类名');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('categories');
}
};
3、执行迁移文件生成数据表
$ php artisan migrate
INFO Running migrations.
2024_04_04_231218_create_categorites_table ..................................................................... 57.63ms DONE
查看表结构
迁移文件是如何记录的?我们可以通过命令进行查看
$ php artisan db:table
┌ Which table would you like to inspect? ──────────────────────┐
│ migrations │
└──────────────────────────────────────────────────────────────┘
migrations ..................................................................................................................
Columns ................................................................................................................... 3
Size ............................................................................................................... 16.00 KB
Column ................................................................................................................. Type
id int, autoincrement .......................................................................................... int unsigned
migration varchar, utf8mb4_unicode_ci .......................................................................... varchar(255)
batch int ............................................................................................................... int
Index .......................................................................................................................
primary id ................................................................................................... btree, primary
回滚迁移文件
你应该注意到迁移文件中有两个方法:up
和 down
。其中,up
用来生成数据表,而down
则是用户回滚操作。
php artisan migrate:rollback
rollback 命令将仅回滚最后一批迁移。
查看数据表
$ php artisan db:show
MySQL ................................................................................................................ 8.0.30
Database ................................................................................................ book_laravel11_blog
Host .............................................................................................................. 127.0.0.1
Port ................................................................................................................... 3306
Username ............................................................................................................... root
URL .........................................................................................................................
Open Connections .......................................................................................................... 4
Tables ................................................................................................................... 10
Total Size ........................................................................................................ 160.00 KB
Table .................................................................................................................. Size
cache .............................................................................................................. 16.00 KB
cache_locks ........................................................................................................ 16.00 KB
categories ......................................................................................................... 16.00 KB
failed_jobs ........................................................................................................ 16.00 KB
job_batches ........................................................................................................ 16.00 KB
jobs ............................................................................................................... 16.00 KB
migrations ......................................................................................................... 16.00 KB
password_reset_tokens .............................................................................................. 16.00 KB
sessions ........................................................................................................... 16.00 KB
users .............................................................................................................. 16.00 KB
请登录后再评论