可以使用下面的命令
php artisan migrate --pretend --no-ansi
當然,你需要有可以 migrate 的東西。
數據庫遷移導出到文件(使用命令)
<?php
namespace App\Console\Commands;
use Illuminate\Contracts\Bus\SelfHandling;
class MigrateToSql implements SelfHandling
{
protected $signature = 'migrate_to_sql';
protected $description = '數據庫遷移轉 sql';
/**
* Execute the command.
*
* @return void
*/
public function handle()
{
$command = PHP_BINARY . ' ' . base_path('artisan') . ' migrate --pretend --no-ansi';
exec($command, $output);
$sql = '';
if (count($output) > 0) {
foreach ($output as $line) {
$sql .= preg_replace('/.*?:/', '', $line) . ";\n";
}
}
$file = database_path('sqls/' . date('Y-m-d H:i:s') . '.sql');
file_put_contents($file, $sql);
}
}
上面的一些處理是把一些無效的信息去掉,如時間戳,這樣最后剩下的就是可以直接執行的 sql 語句了。
