Laravel seeder gives error. Class not found

27,882

Solution 1

You need to create an Eloquent model for that table in order to use Login::create(). You can do that with a simple artisan command:

$ php artisan generate:model Login

This will generate a new Eloquent model in app/models directory which should look like this.

class Login extends Eloquent {

    protected $fillable = [];
    protected $table = 'login';

}

Your code should work after that. If it still doesn't make sure you run composer dump-autoload.

Solution 2

EDIT

Now I see, the problem is with your login class (with earlier question formatting the exact error was illegible). You should look again what's the name of file where you have login class and what's the name of class. The convention is that the file should have name Login.php (with capital letter) and the name of class also should be Login (with capital letter). You should also check in what namespace is your Login class. If it is defined in in App namespace, you should add to your LoginTableSeeder:

use App\Login;

in the next line after <?php

so basically the beginning of your file should look like this:

<?php

    use App\Login;
    use Illuminate\Database\Seeder;

EARLIER ANSWER

You didn't explained what the exact error is (probably the error is for Seeder class) but:

In database/seeds/DatabaseSeeder.php you should run Login seeder like this:

$this->call('LoginTableSeeder');

You should put into database/seeds file LoginTableSeeder.php with capital letter at the beginning.

Now, your file LoginTableSeeder.php file should look like this:

<?php

use Illuminate\Database\Seeder;

class LoginTableSeeder extends Seeder
{
    public function run()
    {

        // your code goes here
    }
}

you need to import Seeder with use at the beginning of file and again class name should start with capital letter.

Now you should run composer dump-autoload and now when you run php artisan db:seed it will work fine.

Solution 3

Just run composer dump-autoload -o for the autoloader to pick up the newly classes because the database folder is not automatically autoloaded with PSR-4.

Solution 4

This worked for me

composer dump-autoload -o 

Solution 5

I have the same problem but you can solve it by adding your namespace:

namespace yournamespace; 
use App\Login;
use Illuminate\Database\Seeder;
Share:
27,882
samhu kiklsk
Author by

samhu kiklsk

php developer, app developer, networking, software engineering

Updated on December 19, 2020

Comments

  • samhu kiklsk
    samhu kiklsk over 3 years

    I'm a newbie in Laravel and and I'm teaching myself how to authenticate from a login table. I have migrated and created the table. Now, I'm trying to seed the data into the login table, but the command prompt is continuously giving me error, which says Fatal Error, class login not found and I have no idea what i have missed. So can anyone please help me. Here is the code that i have, and yes I'm using Laravel 4.3

    <?php
    class loginTableSeeder extends Seeder
    {
        public function run()
        {
            DB::table('login')->delete();
            login::create(array(
                'username'  =>  'sanju',
                'password'  =>  Hash::make('sanju')
                ));
        }
    }
    
    
    ?> 
    
  • samhu kiklsk
    samhu kiklsk over 9 years
    Thank you for your reply. I missed out one thing. I named it this way loginTableSeeder.php, and yes i fixed it and now it looks like this, LoginTableSeeder.php. Now, here is my LoginTableSeeder.php code. <?php class loginTableSeeder extends Seeder { public function run() { DB::table('login')->delete(); Login::create(array( 'username' => 'sanju', 'password' => Hash::make('sanju') )); } } ?>, But, its not fixed yet.
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    @samhukiklsk As I mentioned the problem is probably that Seeder class is not found You need to have use Illuminate\Database\Seeder; line in this file after <?php as in my code above
  • samhu kiklsk
    samhu kiklsk over 9 years
    Hey, i am a bit close to yours. The solution worked but only 50%, now the error is base table or view not found. table hariyali.logins, but my table is just login so where did that logins come from. The extra s come from
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    @samhukiklsk I've edited my answer (answer is at the beginning)
  • Bogdan
    Bogdan over 9 years
    Specify the table name as a class property in the Eloquent model class protected $table = 'login';. I've update my answer.
  • Bogdan
    Bogdan over 9 years
    Eloquent automatically assumes the table name is the lowercase plural version of your model name, so in your case Login model name means it will automatically search for the logins table unless you specify the $table property for your model. You can read the Eloquent Docs to get more aquainted with the conventions.
  • samhu kiklsk
    samhu kiklsk over 9 years
    Ok, now 80% the problem is fixed, now there is one more problem it says unknown column name 'updated_at', do i need to add the timestamp() in my migration file. Is that error all about
  • Bogdan
    Bogdan over 9 years
    Yes you could add that to the migration, but you could also add protected $timestamps = false; in your model class, if you don't want the model to use the default timestamps naming convention.
  • samhu kiklsk
    samhu kiklsk over 9 years
    thanks a lot...thanks man!!It worked i will figure our about the the timestamp() in the laravel document. thanks dude
  • Ankit Vishwakarma
    Ankit Vishwakarma over 7 years
    the document is well and good but this error not mentioned in the documents of laravel. laravel.com/docs/5.3/seeding in my composer.json file autoload class is already there and set database in it. just running composer dump-autoload worked for me
  • Akbar Noto
    Akbar Noto almost 5 years
    what if the escape is disabled by the hosting?