soyhuce / laravel-factories-extended
Laravel模型工厂扩展
0.1.1
2020-11-10 13:47 UTC
Requires
- php: ^7.3|^8.0
- illuminate/database: ^8.0
- illuminate/support: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.17
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.4
- soyhuce/next-ide-helper: ^0.3.0
Suggests
- soyhuce/next-ide-helper: Laravel ide helper rebuilt under steroids, to document Factory extensions
This package is auto-updated.
Last update: 2024-09-10 22:07:33 UTC
README
本软件包为Laravel 8模型工厂提供扩展
安装
您应该使用composer安装此软件包
composer require soyhuce/laravel-factories-extended
安装完成!
使用方法
您的模型工厂必须继承 Soyhuce\LaravelFactoriesExtended\Factory
<?php use Soyhuce\LaravelFactoriesExtended\Factory; class UserFactory extends Factory { // Same like usual factories }
有关工厂的更多文档请参阅这里
扩展
和动态的
有时我们需要在测试中同时使用一个模型及其相关模型。使用of
,您可以为创建的模型和现有模型定义关系
$user = UserFactory::new()->create(); $posts = PostFactory::times(3)->of($user)->create();
如果无法直接从模型中猜测关系,我们可以显式定义它
$posts = PostFactory::times(3)->of($user, 'author')->create();
您还可以使用of
与MorphTo
关系一起使用
$comments = CommentFactory::times(3)->of($post, 'commentable')->create();
以及多个模型
$comments = CommentFactory::times(3)->of($post, 'commentable')->of($user, 'user')->create();
您可以使用动态的of
来定义关系名称
$user = UserFactory::new()->create(); $post = PostFactory::new()->ofAuthor($user)->create(); $comments = CommentFactory::times(3)->ofCommentable($post)->ofUser($user)->create();
如果您使用soyhuce/next-ide-helper
(版本 ^0.2.4),我们提供工厂扩展来自动添加动态的of
方法。您只需在您的next-ide-helper.factories.extensions
配置中添加Soyhuce\LaravelFactoriesExtended\DynamicOfResolver::class
即可。
贡献
欢迎您为此项目做出贡献!请参阅CONTRIBUTING.md。
许可
本软件包在MIT许可证下提供。