Use Factory in laravel

PHOTO EMBED

Wed Jan 04 2023 06:51:38 GMT+0000 (Coordinated Universal Time)

Saved by @ahmad007 #laravel #ajax

1. First make a factory by using the following code 
php artisan make:factory CategoryFactory -model=Category
2. Go to the CategogyFatory file in factories folder and write the following code
 $category_name = $this->faker->unique()->words($nb=2, $asText = true);
        $slug = Str::slug($category_name, '-');
        return [
            'name' => $category_name,
            'slug' => $slug
        ];
3. You can use Str class as on top
  use Illuminate\Support\Str;
4. Now go to the DatabaseSeeder and write the following code in the run function
 \App\Models\Category::factory(6)->create();
5. use the following command
php artisan db:seed
6. You can make as many factories and call in the databaseseeder file and use the factories
Thanks!
content_copyCOPY