You could use regexify and and this let's you control what characters could be in the randomly generated string like this:

<?php

use Faker\Generator as Faker;

$factory->define(App\User::class, function (Faker $faker) {
    return [
        'name' => $faker->regexify('[A-Za-z0-9]{' . mt_rand(4, 20) . '}'),
    ];
});

Another option is $faker->realText(mt_rand(4, 20))

Answer from Latheesan on Stack Overflow
🌐
FakerPHP
fakerphp.org › formatters › numbers-and-strings
Numbers and Strings - FakerPHP / Faker
Generates a random float. Optionally it's possible to specify the amount of decimals. The second and third parameters optionally specify a lower and upper bound respectively. echo $faker->randomFloat(); // 12.9830, 2193.1232312, 102.12 echo ...
🌐
GitHub
github.com › fzaninotto › Faker
GitHub - fzaninotto/Faker: Faker is a PHP library that generates fake data for you · GitHub
<?php // Generate a random Employer Identification Number echo $faker->ein; // '12-3456789'
Starred by 26.7K users
Forked by 3.6K users
Languages   PHP
🌐
Shortcode
shortcode.dev › laravel › faker.html
Laravel cheatsheet > Faker
#laravel#db · Reference: https://github.com/fzaninotto/Faker · $faker = Faker\Factory::create(); $faker->name; // First and second name $faker->randomDigit; // A random number $faker->word; // A single word $faker->sentence; // A sentence $faker->unique()->word; // A single unique word ...
🌐
GitHub
github.com › fzaninotto › Faker › issues › 1512
$faker->unique()->numberBetween(1, 20) · Issue #1512 · fzaninotto/Faker
June 21, 2018 - I'm using Laravel and I have 2 factory states: user and post. I generate 20 users, and 50 posts. Now the interesting part, here are the 2 states, redundent code removed: User: factory(App\Photo::Class, 20)->states('user')->create(); $factory->state(App\Photo::class,'user', function (Faker $faker) { return ([ 'imageable_id' => $faker->unique()->numberBetween(1,20), 'imageable_type' => 'App\User' ]); });
Author   datner
🌐
Fakerphp
fakerphp.github.io
FakerPHP / Faker
// unique() forces providers to return unique values $values = []; for ($i = 0; $i < 10; $i++) { // get a random digit, but always a new one, to avoid duplicates $values []= $faker->unique()->randomDigit(); } print_r($values); // [4, 1, 8, 5, ...
Find elsewhere
🌐
Laravelia
laravelia.com › post › laravel-faker-random-10-digit-number-example
Laravel Faker Random 10 Digit Number Example
December 13, 2022 - laravelia.com · 2025 Copyright | All Rights Reserved. Privacy Policy
🌐
OneLinerHub
onelinerhub.com › php-faker › how-do-i-generate-a-random-number-between-two-numbers-using-laravel-faker
Php Faker: How do I generate a random number between two numbers using Laravel Faker? - OneLinerHub
For example, to generate a random number between 1 and 10, you can use the following code: $randomNumber = Faker::numberBetween(1, 10); echo $randomNumber;
🌐
Laravel Daily
laraveldaily.com › post › generate-random-strings-laravel-helper-methods
Generate Random Strings with Laravel: Helper Methods
May 22, 2024 - If you take a look at the Laravel source code, you may find more parameters to this function: ... So, experiment with true/false values for these parameters and see what happens. Laravel have a fake() helper which resolves to a Faker ...
🌐
Fwhy
fwhy.github.io › faker-docs › formatters › base › random_number.html
randomNumber | FakerPHP非公式リファレンス
November 22, 2023 - 返される整数の桁数を1から9で指定します。 nullを指定した場合、randomDigitNotNull()で生成されたランダムな値になります。 · trueにした場合、返り値が正確に$nbDigits桁になります。 · $strictに真偽値以外を渡した場合、または$maxにmt_getrandmax()より大きい値を渡した場合はInvalidArgumentExceptionが発生します。 · >>> Faker\Factory::create('ja_JP')->randomNumber() => 17 >>> Faker\Factory::create()->radomNumber(9) => 735949189 >>> Faker\Factory::create()->randomNumber(5, true) => 18904 ·
🌐
Shivlab
shivlab.com › home › how to use laravel faker for random date generation
How to Use Laravel Faker for Random Date Generation
September 5, 2024 - By default, Laravel includes Faker in its development dependencies. However, if you want to ensure that it’s installed, you can manually add Faker by running the following command: ... This will install the latest version of the Faker library in your project. Now that you have Faker installed, the next step is to set up a seeder that will generate random dates.