36

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' => '[email protected]',
 '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?

asked May 11, 2015 at 19:35
8
  • Does $this->call('\MemberInvitationSeeder'); make any difference? Commented May 11, 2015 at 19:37
  • Or use App\MemberInvitationSeeder in DatabaseSeeder.php Commented May 11, 2015 at 19:40
  • It just claims \MemberInvitationSeeder isn't found... Commented May 11, 2015 at 19:43
  • use App\MemberInvitationSeeder has no effect. Still not found. Commented May 11, 2015 at 19:44
  • 10
    Did you remember to run composer dump-autoload? Commented May 11, 2015 at 20:00

9 Answers 9

120

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

"classmap": [
 "database"
],
answered Sep 27, 2016 at 9:26

3 Comments

running "composer dump-autoload " only thing i needed to do.
This should be the accepted answer, the current one is based on a false conclusion or missing knowledge.
Cool but a bit OT: You can use your model to seed database. Instead of DB::table('my_foos')->insert(['col1' => 'Value 1']) use MyFoos::create(['col1' => 'Value1']). The benefit is, the auto-maintained created_at and updated_at are being filled out for you. While with DB::table('x')->insert() they are empty.
13

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;
answered Jun 11, 2018 at 11:06

Comments

7

This work for me

  1. composer dump-autoload
  2. php artisan db:seed
answered Jul 12, 2020 at 16:07

Comments

5

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?

answered May 11, 2015 at 20:51

1 Comment

Take a look in your composer.json file. In the autoload section, you should see where it's setting up the classmap and it should have "database" in the array which tells composer to add everything in that folder to the autoloader. If it's not in there, that's your problem. If it is in there, try composer self-update and then composer dump and see if that helps.
2

I ran into the similar error but I was using DB facade DB::table rather than model. I am posting the answer just in case somebody has similar issues.

The seeder file had namespace namespace Database\Seeders; and laravel was trying to look for DB class inside the namespace and hence the error appeared.

Class 'Database\Seeders\DB' not found.

Resolutions:

  • Remove the namespace and run composer dump-autoload
  • OR Add a backslash to \DB::table('stock_categories')->([ ... ]); so Laravel starts looking the DB facade from root (not from the namespace specified)

Eg,

\DB::table('MemberInvitation')->insert( [
 'id' => 'BlahBlah' ,//com_create_guid(),
 'partner_id' => 1,
 'fisrt_name' => 'Thats',
 'last_name' => 'Me',
 'email' => '[email protected]',
 'mobile_phone' => '444-342-4234',
 'created_at' => new DateTime
 ] );
answered Nov 13, 2020 at 12:42

Comments

1

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;
Paul Roub
36.5k27 gold badges88 silver badges95 bronze badges
answered Sep 20, 2018 at 21:49

Comments

0

You should include namespace if you want to use a string as a parameter

 $this->call('Database\Seeders\MemberInvitationSeeder');
answered Oct 18, 2020 at 11:23

2 Comments

There is no need to add path of the seeder file. $this->call([MemberInvitationSeeder::class]); only this is enough
Your way is enough, I agree, but mine works too. Just an althernative
0

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.

answered Nov 13, 2022 at 17:02

1 Comment

Welcome to SO! Please take a look at the other answers that were given before. Your approach is mentioned there already. In order to keep the site clear and make it easy to find answers, we try to avoid double answers.
0

I was facing this problem with laravel package with spatie to solve this problem just

1- append seeders path in composer.json of your package

 "autoload": {
 "psr-4": {
 ...
 "{packagename}\\{vendorname}\\Database\\Seeders\\": "database/seeders"
 }
 },
  1. then go to your main project and update composer with composer update
  2. auto-load your classess with composer dump-autoload

If it doesn't work delete Vendor folder and run composer install

answered Nov 14, 2022 at 13:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.