我已经尝试过了,它也可以工作,但你的解决方案比这更好,@Artur。
class AddTeamIdToNecessaryTables extends Migration
{
protected $table_names;
function __construct()
{
$this->table_names = [
'accounts', 'suppliers', 'expenses', 'draft_invoices', 'quote_jobs',
'committed_invoices', 'quotes', 'rate_plans', 'call_categories',
'prefix_groups', 'draft_items', 'committed_invoice_cdrs', 'committed_items',
'call_data_records', 'committed_temp_xero_infos', 'did_records',
'prefixes', 'prefix_cost_prices', 'purchase_items', 'purchase_orders',
'quote_costs', 'sippy_root_accounts', 'temp_xero_infos', 'sippy_infos', 'committed_jobs'
];
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\DB::beginTransaction();
try {
foreach ($this->table_names as $table_name) {
Schema::table($table_name, function (Blueprint $table) {
$table->integer('team_id')->unsigned()->nullable()->after('id');
$table->foreign('team_id')->references('id')->on('teams');
});
}
\DB::commit();
} catch (\Exception $e) {
\DB::rollback();
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
\DB::beginTransaction();
try {
foreach ($this->table_names as $table_name){
Schema::table($table_name, function (Blueprint $table) {
$table->dropForeign(['team_id']);
$table->dropColumn('team_id');
});
}
\DB::commit();
} catch (\Exception $e) {
\DB::rollback();
}
}
}