是的,你可以做到的
但不推荐
第 1 步:
运行命令 php artisan make:migration create_bulk_table
现在打开database/migrations 中的迁移文件夹,您将找到新的迁移time_stamp_create_bulk_table.php
打开它你会发现这样的内容
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBulkTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bulk', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('bulk');
}
}
怎么做
这里面有两种方法
一个用于creating the table,即up METHOD
和
另一个是dropping the table,即down METHOD
例如如果你想迁移
posts 表,tasks 表,products 表
在单一迁移中
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBulkTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable('posts'))
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('post_name');
$table->text('post_desc');
$table->timestamps();
});
}
if (!Schema::hasTable('tasks'))
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('task_name');
$table->enum('task_status', ['Open', 'Closed','Inactive']);
$table->text('task_desc');
$table->timestamps();
});
}
if (!Schema::hasTable('products'))
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_name');
$table->text('product_desc');
$table->string('product_price');
$table->timestamps();
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
Schema::dropIfExists('tasks');
Schema::dropIfExists('products');
}
}
BE CAREFUL WHILE REFERING THE FORIEGN KEY OF PARENT TABLE WHICK NEEDS TO BE CREATED BEFORE THE REFERAL
例如,您在 post 表中有列 user_id,您所指的就像
$table->foreign('user_id')->references('id')->on('users');
users表需要在posts表之前迁移
希望清楚
如果您发现任何错误,请在下方评论