3

I'm following the docs to seed the users table, which shows User::create being used

class UserTableSeeder extends Seeder {

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

      User::create([
        'username' => 'test',
        'firstname' => 'Test',
        'lastname' => 'Test',
        'email' => 'test@domain.com',
        'password' => Hash::make('test'),
        'role' => 'user'
      ]);
    }

}

But it keeps saying:

PHP Fatal error:  Class 'User' not found in /home/vagrant/projects/roopla/database/seeds/UserTableSeeder.php on line 17

I thought maybe I need to make:model using artisan for User, but it already exists. Can anyone point me in the right direction I'm only just starting out with Laravel and piecing it together through Laracast and the docs, but there's no Laracast on seeding for 5.0. It doesn't seem like you can generate seeds now as artisan doesn't recognize generate:seed

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
mtpultz
  • 17,267
  • 22
  • 122
  • 201

2 Answers2

7

That's because in Laravel 5 User model is in App namespace whereas your seeder is in global namespace.

You should use:

 \App\User::create([
        'username' => 'test',
        'firstname' => 'Test',
        'lastname' => 'Test',
        'email' => 'test@domain.com',
        'password' => Hash::make('test'),
        'role' => 'user'
      ]);

or at the beginning of seeder file:

use App\User;

and now you can use the code you showed

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
1

You need to either import your user model at the top of the seeder with use App\User; or just reference it via \App\User::create.... You need to reference the User model through it's namespace.

Jesse Schutt
  • 396
  • 2
  • 9