Search code examples
laravellaravel-5modellaravel-artisanseeding

Model class not found when running Seeder in Laravel 5


I am creating an Admin auth to my app and I generated the model with the artisan command:

php artisan make:model Admin -m

This is the generated class:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Admin extends Model
{
    //
}

This created an empty model and a basic migration. I added this lines to the migration:

public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 32);
            $table->string('username', 32);
            $table->string('email', 320);
            $table->string('password', 64);            
            $table->string('remember_token', 100)->nullable();
            $table->timestamps();
        });
    }

Then I used the command line to create a seeder:

php artisan make:seeder AdminTableSeeder

And added the seeder to the DatabaseSeeder

public function run()
    {
        $this->call(AdminTableSeeder::class);
    }

But when I run the seeder php artisan db:seed I get a class missing error:

PHP Fatal error: Class 'Admin' not found in /laravelpath/database/seeds/AdminTableSeeder.php on line 15

[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Admin' not found

It seems to be the Admin model.

I've tried to run the fixes composer update and composer dump-autoload but they didn't help.

Anyone knows what is happening? Why do I get this error and how do I fix it?

Extra info: I read somewhere that I should name my app so I executed php artisan app:name MyAppName and it added namespaces everywhere (at least in the Http folder). I'm not sure if it messed up my classes.

@edit 1

This is the AdminTableSeeder

use Illuminate\Database\Seeder;

class AdminTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('admins')->delete();
        Admin::create(array(
            'name'     => 'Victor Ferreira',
            'username' => 'victorferreira',
            'email'    => '[email protected]',
            'password' => Hash::make('123456'),
        ));
    }
}

Solution

  • In your AdminTableSeeder.php

    use App\Models\Admin;
    

    have a look at your Admin model it is under App\Models namespace that means your Admin model resides under app/Models/ folder. If that doesn't works in case your Admin isn't under app/Models folder then remove Models from your Admin model and make the namespace App\Admin