zbm/m2m-seeding
Composer 安装命令:
composer require zbm/m2m-seeding
包简介
seed fake data between many-to-many relation
README 文档
README
this package is useful for seeding fake data between two Models with Many-To-Many relationship by some function
Installation
composer require zbm/m2m-seeding
Usage
Basic usage
use m2m\seeding\M2MSeeding; M2MSeeding::make(FirstModel::class, SecondModel::class, 'relation') ->run();
- first thing call the static function make() with the first and second models you need with the name of relation from first model to second model
- then call run() to seeding the fake data
Factory the models
if you want to seed the models using factories you can call withFactory() function
use m2m\seeding\M2MSeeding; M2MSeeding::make(FirstModel::class, SecondModel::class, 'relation') ->withFactory(10, 10) ->run();
the first parameter is the count of factories in the first model and the second parameter is for second one
Detect number of relations
you can use this functions to determine number of relations between the models
->minRelation(10)
determine the minimum number of relations (the default value is 0).
->maxRelation(10)
determine the maximum number of relations (the default value is 3).
->rangeRelation(2, 5)
determine the minimum and maximum number of relations.
Seed the pivot
if the pivot have some data you can also seed it by using withPivot() function
->withPivot(function (){ return [ 'first_column' => rand(1, 10), 'second_column' => fake()->word, 'third_column' => true, ]; })
this function accept callback function returned array of keys (the column name) and values (the value of column)
Example
First Model
class Post extends Model{ public function reactions(){ return $this->belongsToMany(User::class, 'reactions'); } }
Second Model
class User extends Model{ }
reactions table
- id
- user_id
- post_id
- reaction_type
Seeding
use m2m\seeding\M2MSeeding; M2MSeeding::make(Post::class, User::class, 'reactions') ->withFactory(20, 100) ->rangeRelation(50, 80) ->withPivot(function (){ return [ 'reaction_type' => rand(1, 6) ]; }) ->run();
this lines will create 20 Post and 100 User and each Post has between 50 and 80 reactions and each reaction has type as integer between 1 and 6
zbm/m2m-seeding 适用场景与选型建议
zbm/m2m-seeding 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zbm/m2m-seeding 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zbm/m2m-seeding 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-13