Home > Mobile >  InvalidArgumentException Unknown format "Str" on [Larvel 9.35.1] [php 8.0.9]
InvalidArgumentException Unknown format "Str" on [Larvel 9.35.1] [php 8.0.9]

Time:10-18

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 Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\odel: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 Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;

use App\Models\Categories;
use App\Models\Products;
use Illuminate\Database\Seeder;

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:\xampp\htdocs\eclobber> php artisan db:seed

   INFO  Seeding database.  


   InvalidArgumentException 

  ***Unknown format "Str"***

  at C:\xampp\htdocs\eclobber\vendor\fakerphp\faker\src\Faker\Generator.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:\xampp\htdocs\eclobber\vendor\fakerphp\faker\src\Faker\Generator.php:696
      Faker\Generator::getFormatter("Str")

  2   C:\xampp\htdocs\eclobber\vendor\fakerphp\faker\src\Faker\Generator.php:961
      Faker\Generator::format("Str", [])

CodePudding user response:

The fields are probably not supported by faker. Try something like this:

DB::table('categories')->insert([
            'name' => $faker->name,
            ...
            ...
            ...
        ]);

CodePudding user response:

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)

"require-dev": 
{"fakerphp/faker": "^1.20", 

after doing this its fix automatically.

  • Related