Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 1 year ago.
0

Solved my own problem...... but found another!

Add this to the top of DatabaseSeeder.php "use database\seeds\ContactsTableSeeder;"

Then the call in ContactsTableSeeder.php becomes "$this->call(ContactsTableSeeder::class);"

This way we let PHP do all the work of classes and namespacing.

However "php artisan db:seed" now produces this error..... "Class 'database\seeds\DB' not found in....."

0
Solution

It looks like your TableSeeder needs "use Illuminate\Support\Facades\DB;"

Seeder now works just fine.

Last updated 9 years ago.
0

There's another simple solution here: Put your "ContactsTableSeeder" class into DatabaseSeeder.php like below code:

<?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('UserTableSeeder');
       $this->call('ContactsTableSeeder');
   }

}


class ContactsTableSeeder extends Seeder {

   public function run()
   {
       DB::table('contacts')->delete();

       Contact::create([
           'name' => 'The Name',
           'email' => '[email protected]',
           'charset' => 'utf-8'
       ]);

   }

}

And execute "php artisan db:seed" again! It will work!

0

hi all,

I have the some problem as above

here's my code

//DatabaseSeeder.php

//use database\seeds\InsertUser;
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('UserTableSeeder');
		$this->call('InsertUser');
	}

}
//InsertUser.php

//use Illuminate\Database\Seeder;
//use Illuminate\Database\Eloquent\Model;
//use Illuminate\Support\Facades\DB;

class InsertUser extends Seeder
{
	public function run()
	{
		User::create(
				array(
						'nama'		=> 'This',
						'email'		=>	'[email protected]',
						'password'	=>	Hash::make('ikhsan123'),
					)
			);
	}
}

but it still does not work.. It appears class 'User' not found.

Somebody help me.. please

0

@thisem

Check namespaces, by default DatabaseSeeder are not namespaced, but I bet your User class is under Laravel 5.

0

Try \App\User::create(

0

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly.

Greets from México

georgebroiv liked this reply

1

VictorAvelar

The composer dump-autoload worked perfectly. Thank you!

kajackdfw You can also use the Model at the top if you call it often in the class:

<?php

use Illuminate\Database\Seeder;
use App\User;

class UserTableSeeder extends Seeder {
    public function run()
    {
        User::create(array(
Last updated 8 years ago.
0
Last updated 8 years ago.
0

VictorAvelar said:

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly.

Greets from México

Thanks a lot @VictorAvelar that fixed the problem. It's always simple details like that haha. Cheers mate!

0

That's why by default Lumen Composer file maps the database path with the classmap strategy. That means that every time Composer dumps the autoloader, it creates a static map of all the classes available inside that folder. So every time we add a new class there we need to manually re-dump the autoloader script:

composer dump-autoload

0

VictorAvelar said:

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly.

Greets from México

This is the ultimate solution ! Thanks a lot.

0

Hello guys,

I had the same issue, and every thing you guys suggested I checked all of them, all things are OK. but still I was having the same problem.

And I figure out on my own, that some thing wrong with caching, I assumed some code might be cached and that's why it's not able to find newly created seed class.

So, I just run the command

"composer update"

And it's works for me. Thanks alot, for helping me, and I hope this option may help some one else.

0

In my case I haven't had in my composer.json

"classmap": [ ... "database/migrations" ]

0

VictorAvelar said:

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly. Greets from México

It works! Thanks @VictorAvelar

Last updated 7 years ago.
0
composer dump-autoload
0

ghkdev said:

It looks like your TableSeeder needs "use Illuminate\Support\Facades\DB;"

Seeder now works just fine.

This did the job :+1:

0

composer dump-autoload worked for me !!!

0

VictorAvelar said:

You should never forget, even when it sounds dummy to dump the autoload files with the command "composer dump-autoload" to make the seeders work properly.

Greets from México

Perfect!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

ghkdev ghkdev Joined 25 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.