是否有任何Laravel方法来执行.SQL文件以加载数据 [英] Is there any Laravel way to execute a .SQL file to load data

查看:615
本文介绍了是否有任何Laravel方法来执行.SQL文件以加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要加载到新数据库中的历史数据.

I have historical data that I want to load in new DB.

我可以通过运行MySQL命令来执行此操作,但是我想知道是否有artisan个命令可以执行此操作?

I could do it by running MySQL command but I am interested to know whether there are artisan commands to do it?

推荐答案

没有一种方法可以使用artisan开箱即用地导入数据库转储.但是,您可以创建一个自定义的artisan命令:

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.

这篇关于是否有任何Laravel方法来执行.SQL文件以加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆