简体   繁体   English

Laravel 5:未找到 DB 种子 class

[英]Laravel 5: DB Seed class not found

I have this DatabaseSeeder.php:我有这个 DatabaseSeeder.php:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;



    class DatabaseSeeder extends Seeder {

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

            $this->call('MemberInvitationSeeder');
        }
    }

I have this file MemberInvitationSeeder.php, sibling to the DatabaseSeeder.php file我有这个文件 MemberInvitationSeeder.php,与 DatabaseSeeder.php 文件同级

<?php

use Illuminate\Database\Seeder;
use App\MemberInvitation;

    class MemberInvitationSeeder extends Seeder {

        public function run()
        {
            MemberInvitation::truncate();

            MemberInvitation::create( [
                'id' => 'BlahBlah' ,//com_create_guid(),
                'partner_id' => 1,
                'fisrt_name' => 'Thats',
                'last_name' => 'Me',
                'email' => 'me@mymail.com',
                'mobile_phone' => '444-342-4234',
                'created_at' => new DateTime
            ] );

        }
    }

Now I call现在我打电话

php artisan db:seed

and I get:我得到:

[ReflectionException]                        
  Class MemberInvitationSeeder does not exist

I tried everything I could find including "composer dump-autoload".我尝试了所有我能找到的东西,包括“composer dump-autoload”。 to no avail.无济于事。 What am I doing wrong?我究竟做错了什么?

Step one - generate seed:第一步- 生成种子:

php artisan make:seed MemberInvitationSeeder

Step two - In DatabaseSeeder.php add line:第二步- 在 DatabaseSeeder.php 添加行:

$this->call(MemberInvitationSeeder::class);

Step three :第三步

composer dump-autoload

Step four :第四步

php artisan db:seed

This should work这应该工作


If this isn't the clue, check the composer.json file and make sure you have code below in the "autoload" section:如果这不是线索,请检查 composer.json 文件并确保您在“自动加载”部分有以下代码:

"classmap": [
    "database"
],

I solved this by adding the class to the seeder file, with the instruction use :我通过将类添加到种子文件中解决了这个问题,指令use

<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\YourClassName;

I believe I know the reason now.我相信我现在知道原因了。

The new class MemberInvitationSeeder wasn't in the autoloaded classes in the composer.json file.新类 MemberInvitationSeeder 不在 composer.json 文件的自动加载类中。

It wasn't there because I added that class manually.它不在那里,因为我手动添加了该类。

Now, going forward, if I add such classes again, what should I use in order for my class to automatically to the autoloader?现在,展望未来,如果我再次添加这样的类,我应该使用什么来让我的类自动加载到自动加载器?

这对我有用

  1. composer dump-autoload
  2. php artisan db:seed

I ran into the similar error but I was using DB facade DB::table rather than model.我遇到了类似的错误,但我使用的是 DB Facade DB::table而不是模型。 I am posting the answer just in case somebody has similar issues.我发布答案以防万一有人有类似的问题。

The seeder file had namespace namespace Database\\Seeders;播种机文件具有命名namespace Database\\Seeders;命名namespace Database\\Seeders; and laravel was trying to look for DB class inside the namespace and hence the error appeared.并且 laravel 试图在命名空间中查找 DB 类,因此出现了错误。

Class 'Database\\Seeders\\DB' not found . Class 'Database\\Seeders\\DB' not found

Resolutions:决议:

  • Remove the namespace and run composer dump-autoload删除命名空间并运行composer dump-autoload
  • OR Add a backslash to \\DB::table('stock_categories')->([ ... ]);添加一个反斜杠\\DB::table('stock_categories')->([ ... ]); so Laravel starts looking the DB facade from root ( not from the namespace specified )所以 Laravel 开始从根(而不是从指定的命名空间)查看数据库外观

Eg,例如,

\DB::table('MemberInvitation')->insert( [
         'id' => 'BlahBlah' ,//com_create_guid(),
         'partner_id' => 1,
         'fisrt_name' => 'Thats',
         'last_name' => 'Me',
         'email' => 'me@mymail.com',
         'mobile_phone' => '444-342-4234',
         'created_at' => new DateTime
    ] );

If the above solutions doesn't work, try this one.如果上述解决方案不起作用,请尝试此方法。 You may have changed the namespace (by default, it's "App").您可能更改了命名空间(默认情况下,它是“App”)。 What you need to do is to go to the composer.json file and check this:您需要做的是转到composer.json文件并检查:

"autoload": {
      "classmap": [
          "database"
      ],
      "psr-4": {
          "App\\": "app/"
      }
  },

If the namespace is App like this example, this solution is not for you.如果命名空间是 App 像这个例子,这个解决方案不适合你。

Otherwise, take the namespace you found and insert that line into your seeder class:否则,使用您找到的命名空间并将该行插入到您的播种机类中:

use NameSpaceFound\User;

如果要将字符串用作参数,则应包含命名空间

 $this->call('Database\Seeders\MemberInvitationSeeder');

After making php artisan make:seed SeederName then you need to run composer dump-autoload command after that php artisan db:seed It is working for me.在制作php artisan make:seed SeederName之后,您需要在php artisan db:seed之后运行composer dump-autoload命令它正在为我工作。

I was facing this problem with laravel package with spatie to solve this problem just我正面临这个问题 laravel package 用spatie来解决这个问题

1- append seeders path in composer.json of your package 1- append composer.json 中的 package 播种机路径

 "autoload": {
        "psr-4": {
            ...
            "{packagename}\\{vendorname}\\Database\\Seeders\\": "database/seeders"
        }
    },
  1. then go to your main project and update composer with composer update然后 go 到您的主项目并使用composer update更新作曲家
  2. auto-load your classess with composer dump-autoload使用composer dump-autoload加载你的类

If it doesn't work delete Vendor folder and run composer install如果它不起作用,请删除Vendor文件夹并运行composer install

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM