faulker / eloquent-export
一个CLI工具,用于使用Laravel的Eloquent模型导出和导入数据库数据
v0.1.2
2017-02-18 06:51 UTC
Requires
- illuminate/config: ~5
- illuminate/console: ~5
- illuminate/database: ~5
- nesbot/carbon: ~1.18
This package is not auto-updated.
Last update: 2024-09-15 02:28:10 UTC
README
为Artisan命令提供构建配置文件的能力,用于从数据库中导出使用Eloquent模型的数据。
此工具只在Laravel 5.1上进行了测试,但应该可以在更高版本上工作
使用场景
- 从生产数据库导出一个用户及其所有数据,并将其导入本地数据库进行故障测试/修复。
- 备份数据子集,以便您在测试新功能或更改数据库中存储的数据后可以轻松恢复。
Composer安装
将以下内容添加到您的composer.json文件中
{
"require": {
"faulker/eloquent-export": "dev-master"
}
}
将服务提供者添加到config/app.php文件中
Faulker\EloquentExport\EloquentExportServiceProvider::class,
发布默认配置文件config/eloquent-export.php
php artisan vender:publish
5.1注意事项
Laravel 5.1不支持获取模型转换列的列表,因此您需要将Faulker\EloquentExport\EloquentExportTrait特性添加到任何具有JSON或array转换列的模型中。
use Faulker\EloquentExport\EloquentExportTrait;
class MyModel extends Models
{
use EloquentExportTrait;
...
}
配置文件创建
配置文件结构
'profile_name' => [
'model' => \Name\Space\Root\Model::class,
'relations' => [
'[relation]' => \Name\Space\Relation\Model::class,
'[relation].[child_relation]' => \Name\Space\ChildRelation\Model::class,
],
],
示例
'user_posts' => [
'model' => \Name\Space\EloquentUser::class, // User model (root model)
'relations' => [
'posts' => \Name\Space\Posts::class, // Posts model
'posts.comment' => \Name\Space\Comments::class, // Comments model
],
],
上述配置文件将导出一个用户、所有帖子以及每个帖子的所有评论。
用法
基本参数
php artisan export:eloquent [profile] [path_to_file] [--id=] [--import]
- [profile] - 在
config/eloquent-export.php文件中创建的配置文件名称。 - [path_to_file] - 导出/导入文件。
- [--id=] - 要导出的根模型数据的ID($primaryKey)。例如,如果您正在导出一个用户,那么将是用户的ID。
- [--import] - 导入数据,如果不设置,则从数据库中导出数据。
示例用法
使用user_posts配置文件导出
php artisan export:eloquent user_posts /tmp/export/user_posts.json --id=34342
- 输出文件是JSON格式
使用user_posts配置文件导入
php artisan export:eloquent user_posts /tmp/export/user_posts.json --import