export a full SQL dump of your database
There is no single built-in
php artisan command to export a full SQL dump of your database and data. However, Laravel offers commands to manage your database structure via schema dumps or you can use third-party packages and external system commands for full data export.
1. Exporting Database Schema (
schema:dump)Laravel provides the
schema:dump command to export the current database schema (table structures, not data) to an SQL file. This is primarily used to speed up migrations in large projects. - Dump the schema:
bash
-
This command generates anphp artisan schema:dump[connection-name]-schema.sqlfile (e.g.,mysql-schema.sql) in yourdatabase/schemadirectory. The file includes SQL commands equivalent to your migrations. - Dump schema and prune migrations:
bash
php artisan schema:dump --prune
This command generates the SQL file and then deletes all your existing migration files, as they are no longer needed to build the initial schema.
2. Exporting Full Database (Data + Schema)
For a complete database backup including all data, the recommended approach is to use the
mysqldump command directly or leverage a community package that wraps this functionality. Using
mysqldump (External Command) You can run system-level commands from your terminal, which is the most reliable way to create a full SQL dump:
bash
mysqldump -u [username] -p [database_name] > [filename].sql
- Replace
[username]with your MySQL username. [database_name]is the name of your database.[filename].sqlis the desired output file name and path.- You will be prompted to enter your database password after running the command.
Using a Laravel Package
For an integrated solution within your Laravel application, the popular
spatie/laravel-backup package is widely recommended. It uses mysqldump internally and provides a simple Artisan command to run backups. - Install the package:
bash
-
composer require spatie/laravel-backup - Run the backup command:
bash
php artisan backup:run
This will create a compressed archive containing the SQL dump and save it in the storage/app/backups directory by default. You can find detailed instructions and configuration options on the official Spatie Laravel Backup documentation page