makeabledk / laravel-factory-enhanced Goto Github PK
View Code? Open in Web Editor NEWSupercharge your Laravel tests & seeds with nested relations.
License: Creative Commons Attribution Share Alike 4.0 International
Supercharge your Laravel tests & seeds with nested relations.
License: Creative Commons Attribution Share Alike 4.0 International
Simple question: Can this package also create non-persistent models with their relations?
With the normal faker, it currently is a bit of a pain to create such structures in a non-persistent way, because you cannot work with relations properly:
$users = factory(App\User::class, 3)
->create()
->each(function ($user) {
$user->setRelation('posts', factory(App\Post::class, 3)->make());
$user->posts->each(function ($post) use ($user) {
$post->setRelation('user', $user);
});
});
Note: $user->setRelation('posts', $posts)
because setting it directly via $user->posts = $posts
will set it as attribute and not as relation.
The reason I ask is that for a lot of my code I don't need persisted models (which is a good thing in regards to testing the code, because it is much faster without a database). But above isn't quite practical in regards of creating test data. Especially when the scenario is not as simple as this one-to-many relationship.
Hi.
In FactoryBuilder:376 -> mergeAndExpandAttributes in call_user_func there should be a second parameter with the $attributes. I had my defineAs accepting the attributes as the second parameter in the callback and after i installed this package it blew up.
I will come back with more details.
Laravel version: 5.7
Package version: v1.1
Case:
Lets say we have Post
and Category
one to one relation. Factories will look like
$factory->define(Post::class, function (Faker $faker) {
return [
'name' => $faker->name,
'category_id' => function(){
return factory(Category::class);
},
'text' => $faker->paragraph
];
});
$factory->define(Category::class, function (Faker $faker) {
return [
'name' => $faker->name,
];
});
In case if I generate Post like this
$post = factory(Post::class)
->with('category', ['name' => 'programming'])
->create()
I will get correct Post
with related programming
category. But as side effect I also get another dummy category generated by post factory. So I end up with two categories instead of one (it can be unexpected in tests). The only way to prevent this is to manually set 'category_id'
to null
but this is not elegant and requires to remember about all relations and sub-relations
$post = factory(Post::class)
->with('category', ['name' => 'programming'])
->create(['category_id' => null])
I really like the power that with
provides, but I still want to have default relation generation using factory closures.
Can you help with the way to use with()
alongside with factory closures
without side effects?
Is it also somehow possible to use this library with fakerphp/faker
? Currently, fakerphp/faker
conflicts with fzaninotto/faker
and thus I cannot install this library.
P.S.: even with a fork of this repository it seems it doesn't run well with Laravel 8 ๐
Hi! Thanks for this great factory package. I have a simple question.
How can we generate random number of related models_?
For example:
factory(Show::class)
->with(random_int(2, 10) ,'trackers')
->times(random_int(10, 20))
->create();
This code will create exactly same number of Tracker
's for every Show
.
How can I generate random number of Tracker
's for every Show
This is a suggestion.
How about to apply same philosophy into factory definitions?
Sometimes a factory definition requires a lot of nested function to define a default initial state.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.