viladanbina / laravel-model-json
Laravel 模型 JSON
Requires
- php: ^8.0|^8.1
- spatie/laravel-package-tools: ^1.13
README
简介
The Laravel Model JSON package allows you to export data from a specific model in the form of a JSON file. It is based on the php artisan
command and is easy to use.
安装
To install this package, use the following command
composer require vildanbina/laravel-model-json
使用
导出
The command to export data from a model is php artisan model:export {model}
, where {model}
is the class name of the model you wish to export. After running this command, the data will be automatically saved in the storage/app
folder.
For example, to export data from the User
model, you would run the following command
php artisan model:export User
If your model is located in a different folder, you can specify the exact location, like so
php artisan model:export App\Models\User
选项
选择保存路径
This package also has several options that allow you to customize the export functionality. For example, you can use the --path=public
option to save the JSON data in a different folder. Here's an example
php artisan model:export User --path=public
文件名
By default, the filename of the JSON data is "Model-Timestamp", but you can also specify a custom filename using the --filename=data
option. For example
php artisan model:export User --filename=data
排除导出字段
You can also exclude certain columns from the export by using the --except-fields
option. This is useful if you only want to export certain data from the model. For example
php artisan model:export User --except-fields=id,deleted_at
无时间戳
To exclude the created_at
, updated_at
, and deleted_at
columns from the export, use the --without-timestamps
option. For example
php artisan model:export User --without-timestamps
只选择特定字段
If a model has a large number of columns and you only want to export a subset of them, you can use the --only-fields
option. This allows you to specify which columns you want to include in the export. For example
php artisan model:export User --only-fields=name,email
应用特定作用域到查询
If you wish to apply a scope to the model query because you wish to exclude certain records, you can use the --scope={scope}
option. This allows you to specify a scope for the records you want to include in the export. For example
php artisan model:export User --scope=verified
在你的用户模型中,你将会有以下函数
public function scopeVerified(Builder $query): void { $query->whereNotNull('email_verified_at'); }
关系
您现在可以使用新的选项 --with-relationships={relations}
导出模型及其指定的关系。 {relations}
是关系的名称,如果您想附加多个关系,可以使用 +
符号进行分隔。
例如,如果您想导出 Product 模型及其 Category 关系,可以使用以下命令
php artisan model:export Product --with-relationships=category
如果您想导出 Product 模型及其 Category 和 Supplier 关系,可以使用以下命令
php artisan model:export Product --with-relationships=category+supplier
此外,您可以选择仅导出关系的特定列,使用语法 {relationship_name}:{columns_to_export}
。
例如,如果您想导出 Product 模型及其 Category 关系,并且只导出 Category 的 id 和 name 列,可以使用以下命令
php artisan model:export Product --with-relationships=category:id,name
如果您想将JSON保存为美观版本到文件中,可以使用--beautify
选项或其缩写形式-b
。例如
php artisan model:export User --beautify
#or
php artisan model:export User -b
默认情况下,它将以内联JSON的形式导出。
导入
model:import
命令允许您从JSON文件导入数据并将其存储到数据库中。
例如,要导入User
模型的数据,您将运行以下命令
参数
model
:必需。要导入的模型的名称。path
:必需。JSON文件的路径,必须包含有效的JSON数据。
示例
php artisan model:import User public/Users.json
此命令将存储JSON文件中找到的所有数据到数据库中。
排除导入字段
您可以使用--except-fields
选项排除特定的列,用逗号分隔,例如
php artisan model:import User public/Users.json --except-fields=email_verified_at
您也可以使用--without-timestamps
选项排除时间戳。
仅选择特定字段进行导入
如果您只想存储特定的字段,可以使用--only-fields
选项,用逗号分隔。例如
php artisan model:import User public/Users.json --only-fields=first_name,last_name,email
更新现有记录
您可以使用--update-when-exists
选项来更新数据库中的现有记录,而不是创建重复项,例如
php artisan model:import User public/Users.json --update-when-exists
如果您想根据不同的列来分组更新,可以使用--update-keys option
。记录将根据匹配的现有记录进行更新。
php artisan model:import User public/Users.json --update-when-exists --update-keys=email
注意:要启用更新功能,必须存在--update-when-exists
选项。
关系
除了从JSON导入模型外,此包还允许您导入模型之间的关系。目前支持的关系类型包括
- HasOne
- HasMany
- HasOneThrough
- HasManyThrough
- MorphOne
- MorphMany
- MorphToMany
- MorphTo
- BelongsTo
- BelongsToMany
您可以使用新的--with-relationships={relations}
选项导入模型及其指定的关系。{relations}是关系的名称,如果需要附加多个关系,则可以使用+
符号分隔。
例如,如果您想导入带有Product关系的Category模型,可以使用以下命令
php artisan model:import Category public/Categories.json --with-relationships=products
如果您想导入带有Product和User关系的Category模型,可以使用以下命令
php artisan model:import Category public/Categories.json --with-relationships=products+user
此外,您还可以使用语法{relationship_name}:{columns_to_import}
选择只导入关系中的特定列。
例如,如果您想导入带有Product
关系的Category
模型,并且只想导入Product
的id
和name
列,可以使用以下命令
php artisan model:import Category public/Categories.json --with-relationships=products:id,name
注意:除了上述示例中用于导入带有关系的产品的Category外,Category还会更新JSON中找到的属性。
结论
Laravel Model JSON包是一个有用的工具,可以将特定模型的数据导出为JSON格式。它提供了各种选项来自定义导出过程,使其更符合您的需求。试一试,看看它如何帮助您的项目。
贡献
有关详细信息,请参阅CONTRIBUTING。
安全漏洞
请通过vildanbina@gmail.com电子邮件报告任何安全漏洞,而不是问题跟踪器。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。