You can try by this steps:
- Inside your Banana Model added HasFactory like below:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Banana extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'description'
];
}
- Create Factory
- php artisan make:factory BananaFactory --model=Banana
- After generate BananaFactory go to that path then:
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class BananaFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Banana::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->title,
'description' => $this->faker->text,
];
}
}
- After that run this command:
composer dump-autoload
- then open the terminal and run:
php artisan tinker
Banana::factory()->count(3)->create()
Important: Here is document that related to create factory:
https://laravel.com/docs/8.x/database-testing#creating-factories