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....."
It looks like your TableSeeder needs "use Illuminate\Support\Facades\DB;"
Seeder now works just fine.
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!
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
Check namespaces, by default DatabaseSeeder are not namespaced, but I bet your User class is under Laravel 5.
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
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(
I've the same problem right here http://laravel.io/forum/06-13-2015-how-to-insert-values-with-migration
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!
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
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.
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.
In my case I haven't had in my composer.json
"classmap": [ ... "database/migrations" ]
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
ghkdev said:
It looks like your TableSeeder needs "use Illuminate\Support\Facades\DB;"
Seeder now works just fine.
This did the job :+1:
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!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community