iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌿

How to View SQL Queries in Laravel Migrations

に公開
2024/10/18

I suddenly became curious about what kind of queries are actually executed when running Laravel migrations.

Environment

  • PHP 8.1.2
  • Laravel 9.14.1

Investigation

While I could just look at the database query logs, I'll check the command help to see if there's a way to view them using a migration command.

$ php artisan migrate -h

There is an option like this:

--pretend                    Dump the SQL queries that would be run

Execution

Now, let's run it with that option.

$ php artisan migrate --pretend
Migration table created successfully.
2014_10_12_000000_create_users_table: create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
2014_10_12_000000_create_users_table: alter table `users` add unique `users_email_unique`(`email`)
2014_10_12_100000_create_password_resets_table: create table `password_resets` (`email` varchar(255) not null, `token` varchar(255) not null, `created_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
2014_10_12_100000_create_password_resets_table: alter table `password_resets` add index `password_resets_email_index`(`email`)
2019_08_19_000000_create_failed_jobs_table: create table `failed_jobs` (`id` bigint unsigned not null auto_increment primary key, `uuid` varchar(255) not null, `connection` text not null, `queue` text not null, `payload` longtext not null, `exception` longtext not null, `failed_at` timestamp default CURRENT_TIMESTAMP not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
2019_08_19_000000_create_failed_jobs_table: alter table `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`)
2019_12_14_000001_create_personal_access_tokens_table: create table `personal_access_tokens` (`id` bigint unsigned not null auto_increment primary key, `tokenable_type` varchar(255) not null, `tokenable_id` bigint unsigned not null, `name` varchar(255) not null, `token` varchar(64) not null, `abilities` text null, `last_used_at` timestamp null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
2019_12_14_000001_create_personal_access_tokens_table: alter table `personal_access_tokens` add index `personal_access_tokens_tokenable_type_tokenable_id_index`(`tokenable_type`, `tokenable_id`)
2019_12_14_000001_create_personal_access_tokens_table: alter table `personal_access_tokens` add unique `personal_access_tokens_token_unique`(`token`)

Something came up!
These must be the queries being executed during the migration.

Summary

You can check the execution queries by adding the --pretend option.
However, when this option is used, the queries do not seem to be actually executed, as the tables were not created.

GitHubで編集を提案

Discussion