2

In previous Laravel version I use this in tinker:

php artisan tinker
factory(App\Banana::class, 3)->create();

But in Laravel 8, it gives this error: `PHP Error: Class 'Database/Factories/bananaFactory' not found

How to create dummy data in Laravel 8 using tinker please? Thank you.

No One
  • 553
  • 2
  • 9
  • 24
  • Does this answer your question? [Laravel 8, Model factory class not found](https://stackoverflow.com/questions/63943758/laravel-8-model-factory-class-not-found) – miken32 Nov 23 '20 at 06:19
  • Thank you for your response. I had a look but it did not work. I need it for tinker – No One Nov 23 '20 at 23:58

1 Answers1

10

You can try by this steps:

  1. 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'
    ];
}

  1. Create Factory
- php artisan make:factory BananaFactory --model=Banana
  1. 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,
        ];
    }
}

  1. After that run this command:
 composer dump-autoload
  1. 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

Sok Chanty
  • 1,678
  • 2
  • 12
  • 22