Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spAo's avatar
Level 1

PHP random text in different languages for seeder

Hello guys i'm making seeder for my website and i need for my body column random text in three different langues.

DB::table('episodes')->insert([
                'slug' => 'show-one-season-one-episode' . $i,
                'title' => 'episode-name ' . $i,
                'image' => $fakeDir . '/' . $fakeImg->random(),
                'episode_number' => $i,
                'season_id' => $showOneSeasonOne,
                'body' => '',  <----------   some code for three different languages 
                'view' => '2023213',
                'published_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ]);

i have some code for images to get it randomly i want same but for text

0 likes
14 replies
spAo's avatar
Level 1

i have some english text some russian text and some georgian text and i want this text to come randomly in body field

but this text must come randomly

tykus's avatar
tykus
Best Answer
Level 104

You can dynamically create a new Faker instance with a locale string, e.g.:

$faker = Faker\Factory::create('de_DE');
$faker->realText; // locale-specific text

So in your case:

$locales = ['en_GB', 'ru_RU', 'ka_GE'];

$faker = Faker\Factory::create(Arr::random($locales));
$faker->realText; // locale-specific text
1 like
spAo's avatar
Level 1

hey thnx for asnwer but i'm new in laravel so i need more details how can i put that on my seeder?

spAo's avatar
Level 1

having red lines on Faker\Factory and Arr

tykus's avatar

Red lines are likely namespace/import issues:

use Faker\Factory;
use Illuminate\Support\Arr;

// ...

$locales = ['en_GB', 'ru_RU', 'ka_GE'];

$faker = Faker\Factory::create(Arr::random($locales));

DB::table('episodes')->insert([
	// ...
	'body' => $faker->realText,
	// ...
]);
1 like
spAo's avatar
Level 1

Class 'Database\Seeders\Faker\Factory' not found

but i have so i dont understand why i'm getting that error faker php faker 1 14

tykus's avatar

Ensure both use imports are above the class EpisodeSeeder (or whatever your class is named); I suppose it is not there already - this is PHP 101...

1 like
automica's avatar

@spao

as you have

use Faker\Factory;

then you can just access it via:

$faker = Factory::create(Arr::random($locales));
1 like
spAo's avatar
Level 1

guys thank u so much <3 its working but i have one problem on lang switcher its working on georgian on russian but on english its showing russian language

tykus's avatar

Show your implementation

1 like
spAo's avatar
Level 1

DB::table('shows')->insertGetId([ 'body' => $faker->realText,

]);

DB::table('translations')->insert([ 'table_name' => 'shows', 'column_name' => 'body', 'foreign_key' => '1', 'locale' => 'en', 'value' => $faker->realText, 'created_at' => now(), 'updated_at' => now(), ]);

DB::table('translations')->insert([ 'table_name' => 'shows', 'column_name' => 'body', 'foreign_key' => '1', 'locale' => 'ru', 'value' => $faker->realText, 'created_at' => now(), 'updated_at' => now(), ]);

spAo's avatar
Level 1

i changed code

  ' $fakerGe = Factory::create('ka_GE');
    
    $fakerEn = Factory::create('en_GB');

    $fakerRu = Factory::create('ru_RU');    '

:P and now its works like i wanted big big big big big big big big big thnx <3

1 like

Please or to participate in this conversation.