Is there any Laravel way to execute a .SQL file to load data

扶醉桌前 提交于 2019-12-04 05:35:24

There isn't a way to import a DB dump out-of-the-box using artisan. However, you could create a custom artisan command:

php artisan make:console DbImportCommand

and then have it issue a command like:

DB::unprepared(file_get_contents('full/path/to/dump.sql'));

However, it may be advantageous to create a command that runs a seeder (or set of seeders).

php artisan make:console importHistoricalData

and then have that run specific seeders:

$this->call(OldCompanySeeder::class);
$this->call(OldEmployeeSeeder::class);
// etc....

If you wipe the database at some point, or move to a new environment, it's as simple as just running the seeders again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!