samhu kiklsk
samhu kiklsk

Reputation: 153

Laravel seeder gives error. Class not found

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')
            ));
    }
}


?> 

Upvotes: 14

Views: 30587

Answers (7)

Nikunj Dhimar
Nikunj Dhimar

Reputation: 2386

You need to create Model

$ php artisan generate:model Login

and than in your seeder class Needed to add use App\Models\Login; to my loginTableSeeder file.

Upvotes: 0

rabin
rabin

Reputation: 81

This worked for me

composer dump-autoload -o 

Upvotes: 2

DannyFeliz
DannyFeliz

Reputation: 802

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.

Upvotes: 10

hamidreza samsami
hamidreza samsami

Reputation: 419

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

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

Upvotes: 1

Fernando Kosh
Fernando Kosh

Reputation: 3593

I've experienced the same problem. In my case, the composer was extremely old and after it's update everything runs Ok.

Update composer with the command:

$ composer self-update

Hope it can help others.

Upvotes: 0

Bogdan
Bogdan

Reputation: 44526

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.

Upvotes: 9

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

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.

Upvotes: 15

Related Questions