You could use randomElement()
'user_id' => $faker->randomElement([1, 3]),
https://github.com/fzaninotto/Faker#fakerproviderbase
Answer from Jasper on Stack OverflowThe 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();
...
}
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('********************')
Alternate for generate string without special chars.
$faker->regexify('[A-Za-z0-9]{20}')
Update 2023 for v8
Now moved to https://github.com/faker-js/faker, they changed it again:
faker.number.int(options: number | {
max: number,
min: number
} = {}): number
faker.number.int() // 2900970162509863
faker.number.int(100) // 52
faker.number.int({ min: 1000000 }) // 2900970162509863
faker.number.int({ max: 100 }) // 42
faker.number.int({ min: 10, max: 100 }) // 57
source: https://fakerjs.dev/api/number.html#int
Update 2021
Latest versions changed location of the function from faker.random.number to faker.datatype.number, https://github.com/Marak/faker.js/issues/1156
Original answer
You need to give an object to the function:
faker.datatype.number({
'min': 10,
'max': 50
});
So if you just pass a number, it will set it as the max value. The min value is 0 by default.
This is the implementation of the number function :
this.number = function (options) {
if (typeof options === "number") {
options = {
max: options
};
}
options = options || {};
if (typeof options.min === "undefined") {
options.min = 0;
}
if (typeof options.max === "undefined") {
options.max = 99999;
}
if (typeof options.precision === "undefined") {
options.precision = 1;
}
// Make the range inclusive of the max value
var max = options.max;
if (max >= 0) {
max += options.precision;
}
var randomNumber = options.precision * Math.floor(
mersenne.rand(max / options.precision, options.min / options.precision));
return randomNumber;
}
From Fakerjs github
Whole Number faker. random.number(min,max) Random number between 0 and "range".
faker.random.number(100); //returns 92
faker.random.number({min:5, max:10}); //returns 9
Decimal number faker. finance.amount(min,max,decimal places) Random number between "min" and "max" including decimals to X digits.
faker.finance.amount(9000,10000,4); //returns 9948.8363
Boolean faker. random.boolean()
faker.random.boolean(); //returns true
Array Element faker. random.arrayElement(array[]) Selects a random element from an array of possible values. This function is useful to create custom lists of possibilities.
faker.random.arrayElement(["one","two","three","four"]); //returns "two"
var phTyp = faker.random.arrayElement(["cell","work","home"]); //returns "work"
Object Element faker. random.objectElement(object{}) Selects a random element from an object, selects the value not the keys. This function is useful to create custom lists of possibilities.
faker.random.objectElement({one: 1, two: 2, three: 3}); //returns 3