Getting raw SQL performed by laravel 4 migrations
As an alternative solution that doesn't require setting up any event listeners, you can use the --pretend
option when running the command:
php artisan migrate --pretend
This will dump SQL queries that would be run by the migration, but without actually running the migration. It will output on each line the class name of the migration and query being run from that migration, so for a migration that creates a users
table with a unique email
column, you'll get something like this:
CreateUsersTable: create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null, `password` varchar(60) not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci
CreateUsersTable: alter table `users` add unique users_email_unique(`email`)
This option is present since Laravel 4 up to the newest version of Laravel (which at the time I'm posting this answer is 5.1).
If you add this to the beginning of your Routes.php
file - it will dump all SQL that is run by Laravel:
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
So do that, then run php artisan migrate
- and all the SQL is dumped.
You could then just log the SQL to a file or something instead of doing a var_dump - the possibilities are endless...