inkd
inkd

Reputation: 1509

Class 'User' does not exist?

Getting that error from the command prompt when I try and db:seed. I did run composer dump, and ensured the Seeder namespaces are in my seed files.

I don't get why it is telling me the User class (which I'm sure is referring to the model) doesn't exist, when I have the following in app\User.php

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

}

Here is my UserTableSeeder.php file in database\seeds

<?php

use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{

    public function run()
    {
        DB::table('users')->delete();
        User::create(array(
            'username' => 'admin',
            'password' => Hash::make('secret')
        ));
    }

}

I know that these fields don't match the $fillable variable, but I don't think that would cause the class to not even be recognized. Do I need to add a create() function in this file before I can seed?

Here is my database(timestamp)_create_users_table.php for good measure:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('username');
            $table->string('password');
            $table->rememberToken()->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}

Any idea whats going on? Also, by using my own Users table am I screwing myself from being about to use the built-in auth and whatnot?

Upvotes: 1

Views: 4195

Answers (3)

Emeka Mbah
Emeka Mbah

Reputation: 17545

Modify UserTableSeeder.php to this

<?php

use App\User;

use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{

    public function run()
    {
        DB::table('users')->delete();
        User::create(array(
            'username' => 'admin',
            'password' => Hash::make('secret')
        ));
    }

}

or You can do something like this instead

<?php

use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder {

    public function run()
    {

        DB::table('users')->delete();

        DB::table('users')->insert(
            array(
                 'username' => 'admin',
                'password' => Hash::make('secret')
            )
        );

    }

}

Upvotes: 0

Chris Townsend
Chris Townsend

Reputation: 2716

In your seeder add at the top

Use App\User;

Upvotes: 1

Mathew Tinsley
Mathew Tinsley

Reputation: 6976

You're referencing User in your seeder, but you never imported it. You can either import it by adding use App\User; at the top of your seeder, or you can add the namespace to the reference:

\App\User::create(array( ...

Upvotes: 0

Related Questions