Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class 'User' not found

So I'm trying a basic php artisan db:seed after migrating my database but it keeps returning the title error in cmd -[Symfony\Component\Debug\Exception\FatalErrorException] Class 'User' not found

Things I Have Tried

  • php dump-autoload after updating the class
  • php dump-autoload before running the db:seed function
  • rolling back the migration and then re-running it
  • rolling back the migration and then re-running it with the --seed syntax
  • Change the namespace of the 'Users' File

Below is the migrations

<?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('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

I believe that everything here is correct, and now here is the user class.

<?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'];

}

And now lastly is the all important database seeder

<?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('UserTableSeeder');

        Model::reguard();
    }
}

class UserTableSeeder extends Seeder
{
    public function run()
    {

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

        User::create(['email' => '[email protected]']);

    }
}

So that's it my full syntax, if any more files are required then please request them and I will update my question.

like image 687
Kenziiee Flavius Avatar asked Jun 11 '15 07:06

Kenziiee Flavius


1 Answers

In your DatabaseSeeder in the root namespace you call the Class User. It therefor tries to load the class User. The definition of your class User is however in namespace App. You should therefor use either App\User in your DatabaseSeeder or add at the top of the file use App\User;

DatabaseSeeder

<?php

use App\User;
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('UserTableSeeder');

        Model::reguard();
    }
}

class UserTableSeeder extends Seeder
{
    public function run()
    {

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

        User::create(['email' => '[email protected]']);

    }
}

Since Laravel 8+ the User class is now stored by default in the app/Models directory; so instead of using App\User use App\Models\User above.

Ps. by default Laravel ships with a User model, use that one. In case you removed that model you can use the fallback provided by Laravel:

use Illuminate\Foundation\Auth\User;

On a side note something I find very useful in order to debug artisan output. You should use the flag -vvv which adds extreme verbosity to the output messages including a complete stack trace.

php artisan migrate -vvv
like image 108
Luceos Avatar answered Sep 18 '22 07:09

Luceos