Export mysql database structure laravel command
This is simple command I use for old laravel (when I didn’t use migrations but instead used phpmyadmin on server to add new columns).
Anyway here simple command to export your database schema:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class DatabaseStructureExport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:structure-export';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Export database structure';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info("starting db structure export...");
$username = config('database.connections.'.config('database.default').'.username');
$password = config('database.connections.'.config('database.default').'.password');
$host = config('database.connections.'.config('database.default').'.host');
$database = config('database.connections.'.config('database.default').'.database');
$outputFile = storage_path(sprintf("structure_%s_%s.sql", $database, date('Y-m-d')));
$command = "mysqldump -u$username -p$password -h$host --no-data --skip-lock-tables $database > $outputFile";
$process = new Process($command);
$process->run();
if ($process->isSuccessful()) {
$this->info("Database structure exported to $outputFile");
} else {
$this->error("Error exporting database structure: ".$process->getErrorOutput());
}
}
}
Not sure if someone needs it nowadays, but if you have old laravel project inconsistent with migrations, it is neat command.
Having structure you can import it and use in phpstorm so it will highlight SQL queries.