Laravel 5.6 db:seed throws FatalThrowableError : Class 'APP\Todo' not found

对着背影说爱祢 提交于 2019-12-11 02:36:41

问题


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);
    }
}

回答1:


in TodoTableSeeder.php

change run method to

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



回答2:


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 :)




回答3:


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!




回答4:


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
    }
}


来源:https://stackoverflow.com/questions/49755432/laravel-5-6-dbseed-throws-fatalthrowableerror-class-app-todo-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!