Laravel 5: DB Seed class not found

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-24 21:05:30

问题


I have this 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

<?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". to no avail. What am I doing wrong?


回答1:


Step one - generate seed:

php artisan make:seed MemberInvitationSeeder

Step two - In DatabaseSeeder.php add line:

$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 file composer.json and make sure you have this code in "autoload" section:

"classmap": [
    "database"
],



回答2:


I solved this by adding the class to the seeder file, with the instruction use:

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



回答3:


I believe I know the reason now.

The new class MemberInvitationSeeder wasn't in the autoloaded classes in the composer.json file.

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?




回答4:


If the above solutions doesn't work, try this one. You may have changed the namespace (by default, it's "App"). What you need to do is to go to the composer.json file and check this:

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

If the namespace is App like this example, this solution is not for you.

Otherwise, take the namespace you found and insert that line into your seeder class:

use NameSpaceFound\User;


来源:https://stackoverflow.com/questions/30176194/laravel-5-db-seed-class-not-found

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