lucaciotti/laravel-json-seeder
Composer 安装命令:
composer create-project lucaciotti/laravel-json-seeder
包简介
Create and use JSON files to seed your database in your Laravel applications
README 文档
README
This is a modified version of the brilliant package created by TimoKoerber.
In this version I changed the call to "json_decode" in the Seeder side with the package created by Halaxa "json-machine".
- "json_decode" often causes Allowed Memory Size Exhausted, when it has to process large files (which can often happen in database seeding).
- the "halaxa / json-machine" offers a very easy to use and memory efficient drop-in replacement for inefficient iteration of big JSON files
In addition, I added a few more configurations for the seeding procedure.
myChangelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[1.1.1] - 2021-04-16
Fixed
- Improved "Exception -> File Empty" Read content to each file kept causing "Allowed Memory Size Exhausted"
- Inserted timer calculation for each seeding file
[1.1] - 2021-04-16
First Release
Laravel JSON Seeder
Create and use JSON files to seed your database in your Laravel applications.
This package works both ways!
- Export your database into JSON files
- Use JSON files to seed your database
Installation
Require this package with composer. It is recommended to only require the package for development.
composer require timokoerber/laravel-json-seeder --dev
Next you need to publish the config file and register the required commands with ...
php artisan vendor:publish --provider="LucaCiotti\LaravelJsonSeeder\JsonSeederServiceProvider"
This will create the file config/jsonseeder.php where you can find the configurations.
Next add the JsonSeederServiceProvider to the providers array in config/app.php ...
// config/app.php 'providers' => [ ... LucaCiotti\LaravelJsonSeeder\JsonSeederServiceProvider::class, ... ]
Creating JSON seeds from database
Of course you can create the JSON files manually. But if you already have a good development database, you can easily export it into JSON seeds.
You can create seeds for every table in your database by calling ...
php artisan jsonseeds:create
This will create one JSON file for watch table in your database (i.e. table users -> users.json, table posts -> posts.json, etc.).
If you only want to create a seed of one specific table (i.e. users), call ...
php artisan jsonseeds:create users
Existing files won't be overwritten by default. If you call the command again, a sub-directory will be created and the JSON seeds will be stored there.
If you want to overwrite the existing seeds, use the overwrite option like ...
php artisan jsonseeds:create users -o|--overwrite
or just use the command ...
php artisan jsonseeds:overwrite users
Seeding
Go to your databas/seeds/DatabaseSeeder.php and add the JsonSeeder inside the run() method like this ...
// database/seeds/DatabaseSeeder.php class DatabaseSeeder extends Seeder { public function run() { $this->call(LucaCiotti\LaravelJsonSeeder\JsonDatabaseSeeder::class); } }
You can now call the JSON Seeder with the usual Artisan command ...
php artisan db:seed
Settings & Configurations
Directory
By default your seeds will be written into or read from the directory /database/json. If you want a different directory, you can add the environment variable
JSON_SEEDS_DIRECTORY in your .env file ...
# .env
JSON_SEEDS_DIRECTORY=database/json
Ignoring tables
Some tables in your database might not require any seeds.
If you want to ignore these tables, you can put them into the setting ignore-tables in the /config.jsonseeder.php
// config/jsonseeder.php 'ignore-tables' => [ 'migrations', 'failed_jobs', 'password_resets', ]
If a table in your database is empty, the LaravelJsonSeeder will create a JSON file with an empty array by default. This might be useful if you want your seeds to truncate this table.
If you don't want this, you can change the setting ignore-empty-tables in config/jsonseeder.php, so no JSON seed will be created.
// config/jsonseeder.php 'ignore-empty-tables' => true
Important!!! Do not forget to clear the cache after editing the config file:
php artisan cache:clear
Environments
The environment variable JSON_SEEDS_DIRECTORY might be useful if you are using seeds in Unit Tests and want to use different seeds for this.
- database
- json
- development
- comapanies.json
- users.json
- posts.json
- testing
- users.json
- posts.json
Development
# .env
JSON_SEEDS_DIRECTORY=database/json/development
Testing
// phpunit.xml <phpunit> <php> <env name="JSON_SEEDS_DIRECTORY" value="database/json/testing"/> </php> </phpunit>
Errors & Warnings
| Error | Type | Solution |
|---|---|---|
| Table does not exist! | Error | The name of the JSON file does not match any table. Check the filename or create the table. |
| JSON syntax is invalid! | Error | The JSON text inside the file seems to be invalid. Check if the JSON format is correct. |
| Exception occured! Check logfile! | Error | There seems to be a problem with the Database. Check your system and your default logfile. |
| JSON file is empty! | Error | The JSON file is completely empty, which makes it useless. If it should truncate the table, provide an empty array []. Otherwise delelte it. |
| JSON file has no rows! | Warning | The JSON file contains only an empty array []. This results in a truncated table and might not be intended. |
| Missing fields! | Warning | At least one row in the JSON file is missing a field, that is present in the database table. Check for typos or provide it in the JSON file. |
| Unknown fields! | Warning | At least one row in the JSON file has a field that does not exist in the database. Check for typos or make sure to add it to the database table. |
License
Copyright © Timo Körber
Laravel JSON Seeder is open-sourced software licensed under the MIT license.
lucaciotti/laravel-json-seeder 适用场景与选型建议
lucaciotti/laravel-json-seeder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 269 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「laravel」 「seeds」 「seeder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lucaciotti/laravel-json-seeder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lucaciotti/laravel-json-seeder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lucaciotti/laravel-json-seeder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Create and use JSON files to seed your database in your Laravel applications
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Only seed what you haven't seeded yet.
Test data for laravel-excel-seeder
统计信息
- 总下载量: 269
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-15



