Factory goes wild if amount is not provided (correctly). #36606
Unanswered
ChrisSantiago82
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey,
I'm new the the factory stuff. Not sure if this is an issue or if I'm doing something wrong. My client wants a test environment where he can play around with the application we are building. The idea is that he can create database entries as he needs it.
I made this functionality with Livewire and I have started with the creating users and user roles. The client can select for what franchise he wants to have those users, how many he wants. Real simple. It looks like this:
factory(User::class, $amount) ->create([ 'franchise_id' => $this->selectedFranchise, ]) ->each(function ($user) { $user->Roles()->save(factory(UserRole::class)->states('Admin')->make()); });
My first problem was, the $amount came from an input field, and was an integer inside a text. Like '10'. The Factory wants an integer, and ignores that variable. So far so good. If I enter a real integer, like 10 every works perfectly. 10 Users are added and those 10 users (only them) get the admin role.
If I enter a string integer, or null, then one user is crated (still makes sense), and that user also has the admin role. But every other user that is in the system, also gets the admin role. Somehow, if now integer is provided, the each goes over all users, and not just that one that was created in this moment.
What is my mistake?
Beta Was this translation helpful? Give feedback.
All reactions