Srl*_*rle 4 database-migration laravel
Hy,我对通过迁移在laravel中创建表(通常使用DB)有疑问.
我有类似的东西(来自代码快乐)
<?php
Schema::create('users', function($table) {
$table->increments('id');
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 64);
$table->integer('role');
$table->boolean('active');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
好的,这将创建包含9个字段的表"用户",但我对这个回调感到困惑.首先变量"$ table"是哪个类的实例?有人能解释一下这里发生了什么,分别是如何运作的?
好吧,这是它发生的方式:
Schema::create(),它会创建一个Blueprint对象,该对象链接到您作为参数传递的表名.Closure它将接收此先前创建的Blueprint对象作为$table参数,并对其进行操作.$table对象上调用方法时,它实际上是Grammar根据您的数据库将其连接到类.也就是说,如果您使用的是MySQL数据库,它将使用MySqlGrammar类.这可确保您为所使用的任何数据库获取有效的SQL,而无需担心.如果要查看生成的实际SQL查询,可以将该--pretend选项添加到该migrate命令.我建议将其保存在文件中,这样您就可以更轻松地阅读它.例如:
php artisan migrate --pretend > app/storage/migration.sql
Run Code Online (Sandbox Code Playgroud)
这会将其保存在app/storage/migration.sql文件中.
| 归档时间: |
|
| 查看次数: |
848 次 |
| 最近记录: |