vildanbina/laravel-model-json
Composer 安装命令:
composer require vildanbina/laravel-model-json
包简介
Laravel Model JSON
README 文档
README
Introduction
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.
Installation
To install this package, use the following command:
composer require vildanbina/laravel-model-json
Usage
Export
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
Options
Choose your path to save
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
Filename
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
Except Fields from export
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
Without timestamps
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
Without global scopes
You can remove registered global scopes from the export with the --without-global-scopes option. For example:
php artisan model:export User --without-global-scopes
With hidden
By default, only visible fields are included in the export. To also include all hidden fields in the export, use the --with-hidden option. For example:
php artisan model:export User --with-hidden
This will also apply to any included relation(s) if used in combination with the --with-relationships option.
Select only specific fields
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
Forget data
You can forget data from the export by using the dot notation, accepting wildcards using asterisks. For example:
php artisan model:export Post --forget-data=comments.*.moderated_at
This can be useful if you include relations with the --with-relationships option and you would like to remove chaperone()'d relations from the nested data.
The --forget-data option supports one or more keys, comma separated.
Apply a specific scope to the query
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
On your User model you would have the following method:
public function scopeVerified(Builder $query): void { $query->whereNotNull('email_verified_at'); }
The --scope option also supports one or more arguments to be passed to the scope, comma separated. For example:
php artisan model:export User --scope=byEmail,foo@example.com
On your User model you would have the following method:
public function scopeByEmail(Builder $query, string $email): void { $query->where('email', $email); }
Relationships
You can now export models along with their specified relationships using the new option --with-relationships={relations}. {relations} are the names of the relationships and can be separated by + symbol if you want to attach more than one relationship.
For example, if you want to export a Product model along with its Category relationship, you can use the command:
php artisan model:export Product --with-relationships=category
If you want to export a Product model along with both its Category and Supplier relationships, you can use the command:
php artisan model:export Product --with-relationships=category+supplier
Additionally, you can choose to only export specific columns of the relationship by using the syntax {relationship_name}:{columns_to_export}.
For example, if you want to export a Product model along with its Category relationship and only export the id and name columns of the Category, you can use the command:
php artisan model:export Product --with-relationships=category:id,name
If you want to save JSON in a file as a beautified version, you can use the --beautify option or its shorthand -b. For example:
php artisan model:export User --beautify
#or
php artisan model:export User -b
By default, it will be exported to an inline JSON.
Import
The model:import command allows you to import data from a JSON file and store it in your database.
For example, to import data for the User model, you would run the following command:
Parameters
model: Required. The name of the model to be imported.path: Required. The path to the JSON file, which must contain valid JSON data.
Example:
php artisan model:import User public/Users.json
This command will store all the data found in the JSON file in the database.
Except Fields from importing
You can exclude specific columns by using the --except-fields option, separated by commas, ex:
php artisan model:import User public/Users.json --except-fields=email_verified_at
You can also exclude timestamps by using the --without-timestamps option.
Select only specific fields to import
If you only want to store specific fields, you can use the --only-fields option, separated by commas. Ex:
php artisan model:import User public/Users.json --only-fields=first_name,last_name,email
Forget data
You can forget data from the import by using the dot notation, accepting wildcards using asterisks. For example:
php artisan model:import Post public/Posts.json --forget-data=comments.*.moderated_at
The --forget-data option supports one or more keys, comma separated.
Update existing records
You can update existing records in the database instead of creating duplicates by using the --update-when-exists option, ex:
php artisan model:import User public/Users.json --update-when-exists
If you want to group the updates based on a different column, you can use the --update-keys option. The records will be updated based on the matching existing records.
php artisan model:import User public/Users.json --update-when-exists --update-keys=email
Note: The --update-when-exists option must be present in order for the update feature to be enabled.
Relationships
In addition to importing models from JSON, this package also allows you to import relationships between models. Currently supported relationship types are:
- HasOne
- HasMany
- HasOneThrough
- HasManyThrough
- MorphOne
- MorphMany
- MorphToMany
- MorphTo
- BelongsTo
- BelongsToMany
You can import models along with their specified relationships using the new
option --with-relationships={relations}. {relations} are the names of the relationships and can be separated by +
symbol if you want to attach more than one relationship.
For example, if you want to import a Category model along with its Product relationship, you can use the command:
php artisan model:import Category public/Categories.json --with-relationships=products
If you want to import a Category model along with both its Product and User relationships, you can use the command:
php artisan model:import Category public/Categories.json --with-relationships=products+user
Additionally, you can choose to only import specific columns of the relationship by using the
syntax {relationship_name}:{columns_to_import}.
For example, if you want to import a Category model along with its Product relationship and only import the id
and name columns of the Product, you can use the command:
php artisan model:import Category public/Categories.json --with-relationships=products:id,name
Note: In addition to the assignment that will be done in the above examples for importing a product to a category with relationships, Category will also be updated with the attributes found in the JSON.
Conclusion
The Laravel Model JSON package is a useful tool for exporting data from a specific model in a JSON format. It offers various options to customize the export process and make it more convenient for your needs. Give it a try and see how it can help you in your project.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please e-mail vildanbina@gmail.com to report any security vulnerabilities instead of the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
vildanbina/laravel-model-json 适用场景与选型建议
vildanbina/laravel-model-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.88k 次下载、GitHub Stars 达 124, 最近一次更新时间为 2023 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「model-export」 「json-export」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vildanbina/laravel-model-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vildanbina/laravel-model-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vildanbina/laravel-model-json 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 25.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 124
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-18