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!
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter