通过 Laravel 中的迁移向所有表添加列
Add column to all tables through migration in Laravel
随着项目规模的扩大,我们决定每一个数据都应该属于创建它们的公司。因此,我要添加一列“data_owner_company_id”,指向拥有给定记录的公司。是的,可以生成迁移以将此列添加到每个模型,但这并不可行,因为有 120 多个表和模型。我如何以最小的努力解决这个问题?
对于模型部分,我想我可以很容易地通过继承将它应用到所有模型,但不确定迁移。
TL;博士
如何通过迁移将int列添加到所有表?
数据库:MySQL v8
框架:Laravel 8、PHP 7.3
我不是 100% 确定它会起作用,但它是这样的:
创建扩展 Illuminate\Database\Schema\Blueprint;
的 class
在构造函数中调用父构造函数然后
$this->unsignedBigInteger('data_owner_company_id')->nullable();
在迁移中使用新的 class 而不是默认的 Blueprint
如果您在数据库中找到所有 table 的名字,这很简单,您必须循环并为每个 table.
创建列
尝试使用队列创建列,因为这将是一项需要 120 table 秒的繁重工作。
检查以下代码:
class CreateDataOwnerCompanyIdtoEachTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up ()
{
$columns = 'Tables_in_' . env('DB_DATABASE');//This is just to read the object by its key, DB_DATABASE is database name.
$tables = DB::select('SHOW TABLES');
foreach ( $tables as $table ) {
//todo add it to laravel jobs, process it will queue as it will take time.
Schema::table($table->$columns, function (Blueprint $table) {
$table->unsignedInteger('data_owner_company_id');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down ()
{
$columns = 'Tables_in_' . env('DB_DATABASE');//This is just to read the object by its key, DB_DATABASE is database name.
$tables = DB::select('SHOW TABLES');
foreach ( $tables as $table ) {
//todo add it to laravel jobs, process it will queue as it will take time.
Schema::table($table->$columns, function (Blueprint $table) {
$table->dropColumn('data_owner_company_id');
});
}
}
}
随着项目规模的扩大,我们决定每一个数据都应该属于创建它们的公司。因此,我要添加一列“data_owner_company_id”,指向拥有给定记录的公司。是的,可以生成迁移以将此列添加到每个模型,但这并不可行,因为有 120 多个表和模型。我如何以最小的努力解决这个问题?
对于模型部分,我想我可以很容易地通过继承将它应用到所有模型,但不确定迁移。
TL;博士 如何通过迁移将int列添加到所有表?
数据库:MySQL v8 框架:Laravel 8、PHP 7.3
我不是 100% 确定它会起作用,但它是这样的:
创建扩展 Illuminate\Database\Schema\Blueprint;
在构造函数中调用父构造函数然后
$this->unsignedBigInteger('data_owner_company_id')->nullable();
在迁移中使用新的 class 而不是默认的 Blueprint
如果您在数据库中找到所有 table 的名字,这很简单,您必须循环并为每个 table.
创建列尝试使用队列创建列,因为这将是一项需要 120 table 秒的繁重工作。
检查以下代码:
class CreateDataOwnerCompanyIdtoEachTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up ()
{
$columns = 'Tables_in_' . env('DB_DATABASE');//This is just to read the object by its key, DB_DATABASE is database name.
$tables = DB::select('SHOW TABLES');
foreach ( $tables as $table ) {
//todo add it to laravel jobs, process it will queue as it will take time.
Schema::table($table->$columns, function (Blueprint $table) {
$table->unsignedInteger('data_owner_company_id');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down ()
{
$columns = 'Tables_in_' . env('DB_DATABASE');//This is just to read the object by its key, DB_DATABASE is database name.
$tables = DB::select('SHOW TABLES');
foreach ( $tables as $table ) {
//todo add it to laravel jobs, process it will queue as it will take time.
Schema::table($table->$columns, function (Blueprint $table) {
$table->dropColumn('data_owner_company_id');
});
}
}
}