如何在laravel中获取所有模型/迁移名称并添加单个新列

时间:2017-07-15 19:20:51

标签: mysql laravel-5 laravel-migrations artisan-migrate

我在数据库中有88个表,我需要在每个表中有一个新列。单个解决方案是我进行迁移并为所有表编写88个函数以添加新列以及88以删除该列。< / p>

enter image description here

有没有办法在1变量中获取所有表名,然后使用foreach通过单个代码添加新列?

  1. 获取变量

  2. 中的所有表名
  3. 通过foreach循环向该特定变量添加新列

  4. $allTables=?
    foreach($allTables as $singleTable){
        add new column
    }
    

    但是怎么样? 我想在所有表中添加team_id,但我需要一个最佳解决方案。

    我正在使用laravel 5.4版本。

2 个答案:

答案 0 :(得分:2)

请尝试以下操作。

// Get the default database name
$dbName = config('database.connections.' . config('database.default') . '.database');

$tables = DB::select('SHOW TABLES');

foreach($tables as $table)
{
    Schema::table($table->{'Tables_in_' . $dbName}, function($table) {
        // Add the column
        $table->string('some_column');
    });
}

要了解正在进行的分析$ tables变量,但应该非常简单。

答案 1 :(得分:1)

我试过这个并且它也在工作,但是your solution比那更好,@ Artur。

jQuery(document).ready(function ($) {
        $(window).on("resize", function (e) {
            checkScreenSize();
        });

        checkScreenSize();

        function checkScreenSize(){
            var newWindowWidth = $(window).width();
            if (newWindowWidth < 1000) {
                //$('.v-align').flexVerticalCenter();
                console.log("do the flexVerticalCenter()");
            }
            else {
                //$('.v-align').css('margin-top', '0');
                console.log("do the margin-top");
            }
        }
        
        
    });