Faker offers a couple of methods that let you replace placeholders in a given string with random characters:
- lexify - takes given string and replaces ? with random letters
- asciify - takes given string and replaces * with random ascii characters
- numerify - takes given string and replaces # with random digits
- bothify - combines the lexify and numerify
You could try to use one of them, depending on the requirements you have for that random string you need. asciify uses the largest set of characters as replacement so using that one makes most sense.
The following will give you a random string of 20 ascii characters:
$faker->asciify('********************')
Answer from jedrzej.kurylo on Stack OverflowFaker offers a couple of methods that let you replace placeholders in a given string with random characters:
- lexify - takes given string and replaces ? with random letters
- asciify - takes given string and replaces * with random ascii characters
- numerify - takes given string and replaces # with random digits
- bothify - combines the lexify and numerify
You could try to use one of them, depending on the requirements you have for that random string you need. asciify uses the largest set of characters as replacement so using that one makes most sense.
The following will give you a random string of 20 ascii characters:
$faker->asciify('********************')
Alternate for generate string without special chars.
$faker->regexify('[A-Za-z0-9]{20}')
The factory is the real issue here not the faker. Calling of factory I mean.
Let's say you have User and User_Information model for example since you have not mention any models in your question above.
I assume you call the factory like below in which it creates a model one by one separately up to 10 that makes unique() of faker useless.
\App\Models\User_Information::factory()->create(10);
My solution to this problem is to use a loop to make unique() functional.
$max = 10;
for($c=1; $c<=$max; $c++) {
\App\Models\User_Information::factory()->create();
}
NOTE:
$maxmust not be greater toUser::count(), else it will return anOverflowExceptionerror.
In my case I had a setup like this
class DomainFactory extends Factory {
protected $model = Domain::class;
public function definition() {
return ['name' => $this->faker->unique()->domainWord()]
}
}
// Seeder
for ($i = 0; $i < 10; $i++) {
$domain = Domain::factory()->create();
...
}
Which did NOT generate unique values for name because I basically create a new factory and with that a new faker in each loop run. I had to pull the factory out of the loop:
// Seeder
$factory = Domain::factory();
for ($i = 0; $i < 10; $i++) {
$domain = $factory->create();
...
}
Try this:
'parent_id' => $faker->boolean(50) ? Page::orderByRaw('RAND()')->first()->id : null,
Essentially we're saying, order by random, get the first and then get it's id.
boolean(50) should give you a 50% chance of true, so 50% false.
Given "Parent" as the parent model, I do this:
- first seed the Parent;
- seed the Child table using this code in the factory:
'parent_id' => $faker->optional()->randomElement(App\Parent::all()->pluck('id'))
It works because faker's randomElement() takes an array which you populate with all and only the 'id' values of the parent table.
The optional() faker's modifier does or doesn't put a NULL in the parent_id at random. As stated in faker's GitHub, optional() sometimes bypasses the provider to return a default value instead (which defaults to NULL). You can also specify the probability of receiving the default value and the default value to return.
NB: you can't do anything if the Parent table isn't seeded. If so, consider the answer of dlnsk.