We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$faker->optional()
$faker->optional() is only getting called ONE time per Seeder. So the results are either ALL filled or ALL empty instead of healthy mix of each.
<?php // tests/factories/TimeEntryFactory.php /** @var Closure $factory */ $factory('App\TimeEntry', [ 'user_id' => 'factory:App\User', 'comment' => $faker->optional($weight = 0.5)->sentence() ]);
and
<?php // database/seeds/TimeEntryTableSeeder.php use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Laracasts\TestDummy\Factory as TestDummy; class TimeEntryTableSeeder extends Seeder { public function run() { DB::table('time_entry')->delete(); TestDummy::times(100)->create('App\TimeEntry'); } }
You would expect about 50/100 entries to have null "comments", instead it's randomly either 0 or 100.
null
Using for loop instead of times() did not work.
times()
Using a for loop, pulling in Faker, overriding the factory did.
Faker
<?php // database/seeds/TimeEntryTableSeeder.php use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Faker\Factory as Faker; use Laracasts\TestDummy\Factory as TestDummy; class TimeEntryTableSeeder extends Seeder { protected $faker; function __construct(Faker $faker) { $this->faker = $faker->create(); } public function run() { DB::table('time_entry')->delete(); for ($i = 0; $i <= 100; $i++) { TestDummy::create( 'App\TimeEntry', ['comment' => $this->faker->optional($weight = 0.5)->sentence()] ); } } }
With this solution, the results are no longer ALL or NOTHING, it looks 50/50.
The text was updated successfully, but these errors were encountered:
What about using a closure instead? Would that not solve the issue or am I thinking in the wrong track?
Sorry, something went wrong.
I can't see why that'd make a difference though, since the create method runs a for loop over times
times
Hi @bionikspoon,
Your workaround was very helpful. Thanks for this
Pete
No branches or pull requests
$faker->optional()
is only getting called ONE time per Seeder. So the results are either ALL filled or ALL empty instead of healthy mix of each.Problem
Isolated example:
and
You would expect about 50/100 entries to have
null
"comments", instead it's randomly either 0 or 100.Solution:
Using for loop instead of
times()
did not work.Using a for loop, pulling in
Faker
, overriding the factory did.Work around:
With this solution, the results are no longer ALL or NOTHING, it looks 50/50.
The text was updated successfully, but these errors were encountered: