I have the simple Factory below. The problem is that my tax field always gets filled with the same number as the deposit field. This of course results in much too high tax rates.
It seems like faker is caching results between method calls? That seems un-intuitive and I wonder how to disable that. Any hints?
Can you show how you determined that the values are identical?
🤦♂️ I was about to write a long post on how none of the suggestions here work, when I actually looked into the database and the values were different! It was a stupid copy'n'paste error in my template and I output the same column twice. I could have sworn to have checked that.
Thanks for all the suggestions and sorry for wasting everyone's time.
@tisuchi
Using unique() here makes the value unique in the column, not in the row, or am I wrong?
Even if he used it, he could still get the same value for deposit and tax, only the deposit value won't be repeated in the column, correct?
@Moutee If I understand you correctly the unique() method in Laravel's factories ensures that the generated value for a specific field is unique across all records for that factory instance.
Unfortunately, it is not column-specific but applies across all generated records.
@splitbrain If you create many models in one go (i.e. using Property::factory()->count(10)->create() then yes, I find Faker re-uses values instead of generating distinct values for each record. You can get around this by either using the unique modifier as @tisuchi suggests:
'tax' => fake()->unique()->randomFloat(2, 0, 30),
Or by returning the result from a closure. Using a closure will force the value to be re-evaluated for each individual model created: