2
votes

i am using faker generate to generate fake entries and insert it into database with the help of php artisan db:seed when i run this command it shows an error :

Seeding: TodosTableSeeder

Symfony\Component\Debug\Exception\FatalThrowableError : Class 'APP\Todo' not found

at /var/www/html/todos/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:217 213| if ($this->amount < 1) { 214| return (new $this->class)->newCollection(); 215| } 216|

217| $instances = (new $this->class)->newCollection(array_map(function () use ($attributes) { 218| return $this->makeInstance($attributes); 219| }, range(1, $this->amount))); 220| 221| $this->callAfterMaking($instances);

Exception trace:

1 Illuminate\Database\Eloquent\FactoryBuilder::make([]) /var/www/html/todos/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:167

2 Illuminate\Database\Eloquent\FactoryBuilder::create() /var/www/html/todos/database/seeds/TodosTableSeeder.php:15

Please use the argument -v to see more details.

here is my code UserFactory.php

<?php

use Faker\Generator as Faker;

$factory->define(App\User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
        'remember_token' => str_random(10),
    ];
});

$factory->define(App\Todo::class, function(Faker $faker) {
    return[
        'todos' => $faker->sentence(10)
        ];

});

TodosTableSeeder.php

<?php
use App\Todo;
use Illuminate\Database\Seeder;


class TodosTableSeeder extends Seeder
{

    public function run()
    {
        factory(APP\Todo::class, 10)->create() ;
    }
}

DatabaseSeeder.php

    <?php
use App\Todo;
use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{

    public function run()
    {
        //$this->call(UsersTableSeeder::class);
            $this->call(TodosTableSeeder::class);
    }
}
5
Try factory(App\Todo::class, 10)->create() ; instead. Capitalization matters.aynber
i used it in TodosTableSeeder . what you mean by Capitalization matters?Ashar Khan
Meaning APP is not the same as Appaynber

5 Answers

2
votes

in TodoTableSeeder.php

change run method to

public function run()
{
    factory(Todo::class, 10)->create();
}
1
votes

You are try to get Class with APP\Todo and APP\Todo not exist because not is correct , the correct form is \App\Todo::class

But if you call the class in the header, when you need the class you just need to call it like this: class All :: class

I hope this help you :)

0
votes

i got the same issue. The problem was i changed my project name so it can't find App\Todo. Replace App with your project name as MYTODO/Todo::class.

TodoTableSeeder.php

public function run()
{
factory(MYTODO\Todo::class, 10)->create();
}

I hope this helps you!

0
votes
use App\Todo;
use Illuminate\Database\Seeder;


class TodosTableSeeder extends Seeder
{

    public function run()
    {
        //factory(APP\Todo::class, 10)->create() ;
      factory(App\Todo::class, 10)->create() ;
    //I changed the APP to App
    }
}
0
votes

You need to make a model of your todos app.

In this case, the compiler is looking for App/Todo: means it is looking for todo.php in App folder...

artisan make:model Todo

^Run this command first.. then you'll see a Todo.php in your App folder. after that run your original command.

php artisan db:Seed

Hope it works. worked for me in laravel 7.0.