I have create a category Table i want to feed some fake date using Faker and Factory after doingusing php artisan db:seed i get error of InvalidArgumentException Unknown format "Str"
i have try each format as per itellisense suggest as well as syntax as per Laravel – 9 in both case i get same error i have try each things but i didn’t get any satisfactory resolution if any can help please let me know here is my and code CategoryFactory.php and DatabaseSeeder.php and below this error which i got.
*This is my CategoryFactory code
<?php
namespace DatabaseFactories;
use IlluminateDatabaseEloquentFactoriesFactory;
use IlluminateSupportStr;
/**
* @extends IlluminateDatabaseEloquentFactoriesFactory<AppModelsodel:Categories>
*/
class CategoriesFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
$cname = $this->faker->unique()->words($nb = 2, $asText = true);
// $cslug = Str::slug($cname, $separator = '-', $language = 'en' );
$cslug = Str::slug($cname, '-');
return [
'category_name' => $cname,
'category_slug' => $cslug
];
}
}
DatabaseSeeder.php code
<?php
namespace DatabaseSeeders;
// use IlluminateDatabaseConsoleSeedsWithoutModelEvents;
use AppModelsCategories;
use AppModelsProducts;
use IlluminateDatabaseSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
Categories::factory()
->count(6)
->create();
Products::factory()
->count(16)
->create();
}
}
**
i get error of this
**
InvalidArgumentException
PS C:xampphtdocseclobber> php artisan db:seed
INFO Seeding database.
InvalidArgumentException
***Unknown format "Str"***
at C:xampphtdocseclobbervendorfakerphpfakersrcFakerGenerator.php:731
727▕ return $this->formatters[$format];
728▕ }
729▕ }
730▕
➜ 731▕ throw new InvalidArgumentException(sprintf('Unknown format "%s"', $format));
732▕ }
733▕
734▕ /**
735▕ * Replaces tokens ('{{ tokenName }}') with the result from the token method call
1 C:xampphtdocseclobbervendorfakerphpfakersrcFakerGenerator.php:696
FakerGenerator::getFormatter("Str")
2 C:xampphtdocseclobbervendorfakerphpfakersrcFakerGenerator.php:961
FakerGenerator::format("Str", [])
2
Answers
i have deleted all the related files in factories, and model and re created it again and i also upgrade my PHP Version from 8.0.9 to 8.1.1 and i have change in composer.json file "fakerphp/faker": "^1.9.1"(php 8.0.9)
after doing this its fix automatically.
The fields are probably not supported by faker. Try something like this: