Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model Factory generating Latin results, not English and not accepting any other locale

I've built my model factory and it's working nicely, however, the generated sentences, words and other strings are in Latin, and I've searched the following solutions (which aren't working for me):

  • ModelFactory.php

     $factory->define(App\Models\User::class, function (Faker\Generator $faker) 
    {
         $faker->locale = "en_US"; // nothing changed
         return [
            .....
         ];
    }
    
  • AppServiceProvider.php, or even DatabaseServiceProvider.php

     $this->app->singleton(\Faker\Generator::class, function () {
        return \Faker\Factory::create('en_US');
     });
    

And still non-English results like: Soluta doloremque in consequatur.

What is strange is that in vendor/fzaninotto/faker/src/Faker/Factory.php class

the first line is const DEFAULT_LOCALE = 'en_US';

with create function public static function create($locale = self::DEFAULT_LOCALE)

Do I've to run something before doing some change?

I need help I've reached a dead end with this!

Update:

When I run the factory on User model it runs with locale sat in AppServiceProvider and the default it truly English, however, the other model that I got under User in ModelFactory.php is the one with Latin results only.

here is the code for both:

// English, settable
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
    ];
});

// Obligately Latin   
$factory->define(App\Models\Application::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->sentence('3'),
        'description' => $faker->paragraph,
        'field_id' => $faker->numberBetween(1,3),
        'published_at' => $faker->dateTimeBetween('-1 years'),
        'icon_url' => $faker->imageUrl(256, 256),
        'cover_url' => $faker->imageUrl(888, 444),
        'android_url' => $faker->url,
        'ios_url' => $faker->url,
        'windows_url' => $faker->url,
        'android_download_count' => $faker->randomDigit,
    ];
});

And I've compared those two models, nothing suspicious with it.

like image 303
Mo Kawsara Avatar asked Oct 27 '25 07:10

Mo Kawsara


2 Answers

It's true that the Faker package included in Laravel does not have an out-of-the-box solution for this, but for what it's worth, there is a 'hack' workaround for this limitation.

You won't get beautiful sentences, but if you use Faker's company->catchPhrase and company->bs methods, you can string together some pretty humorous english-language sentences, even if they don't make any sense. But if you're purely looking for strings of English words and don't care about their content, utilizing these two methods might help you out!

In Laravel, I have used the following code to make some nice English titles to my seed articles for a news website:

$title = ucwords($faker->catchPhrase .' '.$faker->bs);
like image 56
eResourcesInc Avatar answered Oct 29 '25 03:10

eResourcesInc


This is a limitation of the faker package which Laravel uses to generate database seeds.

Both "sentence" and "paragraph" are included in the Lorem provider of faker. This is exclusively Latin language and cannot be changed to another language.

like image 26
wyattis Avatar answered Oct 29 '25 03:10

wyattis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!