Migration on multiple tables with the same prefix

1

Is it possible with laravel to generate a migration that changes multiple tables with the same prefix? for example: in tables pesquisas_187 and pesquisas_146 (tables that have the same fields)

Schema::table('pesquisas_', function (Blueprint $table) {
   $table->string('code')->after('name');
});
    
asked by anonymous 02.03.2017 / 20:31

1 answer

2

Nothing is ready in the for this purpose , but nothing prevents the encoding of a Migration > with the following code:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Padrao1 extends Migration
{
    public function up()
    {
        $sql = 'SELECT TABLE_NAME as name FROM information_schema.tables ';
        $sql .= 'where TABLE_SCHEMA=? and TABLE_NAME LIKE ? ';
        $result = DB::select($sql, [getenv('DB_DATABASE'), 'pesquisas_%']);
        foreach($result as $table)
        {
            Schema::table($table->name, function (Blueprint $table) {
                $table->string('code')->after('name');
            });
        }
    }

    public function down()
    {

    }
}

In this code a search is done on the tables of the database configured in the .env file with the key DB_DATABASE ( getenv('DB_DATABASE') ) with a filter on the tables of the pesquisas_ prefix, and its return is a list of all the tables that belong to this prefix, the rest is equal using Schema::table , as demonstrated in the code.

References

03.03.2017 / 00:42