简体   繁体   English

运行php Artisan db:seed时未找到Laravel 5类DatabaseSeeder

[英]Laravel 5 class DatabaseSeeder not found when running php Artisan db:seed

I'm new to Laravel, When I run php artisan db:seed i recieve the following message: 我是Laravel的新手,当我运行php artisan db:seed时,我收到以下消息:

[ReflectionException] Class DatabaseSeeder does not exist [ReflectionException]类DatabaseSeeder不存在

I already have run composer dump-autoload sadly without any result. 我已经不幸地已经运行了作曲家dump-autoload而没有任何结果。 My class is located in the default folder /seeds 我的课位于默认文件夹/ seeds中

The code from the class: 来自该类的代码:

<?php namespace database\seeds;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;
use App\Country; 

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        $this->call('CountryTableSeeder');
        $this->call('UserTableSeeder');
    }


}

class UserTableSeeder extends Seeder {

    public function run() {
        DB::table('users');

        User::Create(['username' => 'Bart', 'email'=>'bart@example.com', 'password' => Hash::make('password')]); 
    }
}

class CountryTableSeeder extends Seeder { 
    public function run() {
        DB::table('country');

        Country::Create(['country_name' => 'Nederlander']);
    }
}

What I'm doing wrong? 我做错了什么?

First you should remove namespace database\\seeds; 首先,您应该删除namespace database\\seeds; as @JoeCoder suggested in comment. 正如@JoeCoder在评论中建议的那样。

And the second thing is that you should not put many classes inside one file (as you probably did looking at your question). 第二件事是,您不应将多个类放在一个文件中(就像您在看问题时所做的那样)。 Each class should be placed in separate file. 每个类应放在单独的文件中。

if you've tried everything else and still getting Class DatabaseSeeder does not exist , take a look at your composer.json 如果您已经尝试了所有其他方法,但是仍然Class DatabaseSeeder does not exist ,请查看composer.json

There should be a section that looks something like: 应该有一个类似于以下内容的部分:

    "autoload": {
        "classmap": [
            "app/Library",
            "app/Models"
        ],
...

You should add two more lines here to tell autoloader where else it should look for your classes. 您应该在这里再添加两行,以告诉自动加载器在其他地方查找您的类。

So final result will be: 因此最终结果将是:

    "autoload": {
        "classmap": [
            "app/Library",
            "app/Models",
            "database/seeds",
            "database/migrations"
        ],
...

This will add both seeds and migrations directory to your autoloader class path. 这会将seedsmigrations目录都添加到您的自动加载器类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行php artisan db:seed后在laravel中找不到类“ App \\ Eloquent” - Class 'App\Eloquent' not found in laravel after running php artisan db:seed Lumen 5.6 - php artisan db:种子得到错误&#39;Class DatabaseSeeder不存在&#39; - Lumen 5.6 - php artisan db:seed got error 'Class DatabaseSeeder does not exist' Laravel 7 - 工匠种子目标 class [DatabaseSeeder] 不存在 - Laravel 7 - artisan seed Target class [DatabaseSeeder] does not exist Laravel - 未找到列:运行 php artisan db:seed 后出现 1054 未知列错误 - Laravel - Column not found: 1054 Unknown column error after running php artisan db:seed “UserTableSeeder”类不存在 - Laravel 5.0 [php artisan db:seed] - Class 'UserTableSeeder' does not exist - Laravel 5.0 [php artisan db:seed] php artisan db:种子不起作用Laravel 5 - php artisan db:seed is not working Laravel 5 当我通过DatabaseSeeder调用Laravel Seeder类时找不到 - Laravel Seeder class not found when I call it via DatabaseSeeder Laravel 5:未找到 DB 种子 class - Laravel 5: DB Seed class not found 执行 db:seed Artisan 命令时出现错误“未找到类 &#39;ZipArchive&#39;” - Error "Class 'ZipArchive' not found" occures when the db:seed Artisan command is executed Laravel:“php artisan db:seed”不起作用 - Laravel :“php artisan db:seed” doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM