php Laravel 播种机给出错误。未找到课程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26345408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:51:01  来源:igfitidea点击:

Laravel seeder gives error. Class not found

phpsqllaravellaravel-5artisan

提问by samhu kiklsk

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 foundand 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

我是 Laravel 的新手,我正在自学如何从登录表进行身份验证。我已经迁移并创建了表。现在,我正在尝试将数据播种到登录表中,但命令提示符不断给我错误,这说明Fatal Error, class login not found我不知道我错过了什么。所以任何人都可以请帮助我。这是我拥有的代码,是的,我使用的是 Laravel 4.3

<?php
class loginTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('login')->delete();
        login::create(array(
            'username'  =>  'sanju',
            'password'  =>  Hash::make('sanju')
            ));
    }
}


?> 

采纳答案by Bogdan

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:

您需要为该表创建一个 Eloquent 模型才能使用Login::create(). 你可以用一个简单的 artisan 命令来做到这一点:

$ php artisan generate:model Login

$ php artisan generate:model Login

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

这将在app/models目录中生成一个新的 Eloquent 模型,它应该如下所示。

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.

您的代码应该在那之后工作。如果它仍然不能确保您运行composer dump-autoload.

回答by Marcin Nabia?ek

EDIT

编辑

Now I see, the problem is with your loginclass (with earlier question formatting the exact error was illegible). You should look again what's the name of file where you have loginclass 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 Loginclass. If it is defined in in Appnamespace, you should add to your LoginTableSeeder:

现在我明白了,问题出在你的login班级上(前面的问题格式化了确切的错误是难以辨认的)。您应该再次查看您拥有login类的文件的名称是什么以及类的名称是什么。约定是文件应该有名字Login.php(大写字母),类的名字也应该是Login(大写字母)。您还应该检查您的Login类是什么命名空间。如果它在App命名空间中定义,您应该添加到您的LoginTableSeeder

use App\Login;

in the next line after <?php

在下一行之后 <?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 Seederclass) but:

您没有解释确切的错误是什么(可能是Seeder类的错误)但是:

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

database/seeds/DatabaseSeeder.php你应该像这样运行登录播种机:

$this->call('LoginTableSeeder');

You should put into database/seedsfile LoginTableSeeder.phpwith capital letter at the beginning.

您应该以大写字母开头database/seeds归档LoginTableSeeder.php

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

现在,您的文件LoginTableSeeder.php文件应如下所示:

<?php

use Illuminate\Database\Seeder;

class LoginTableSeeder extends Seeder
{
    public function run()
    {

        // your code goes here
    }
}

you need to import Seederwith useat the beginning of file and again class name should start with capital letter.

你需要在文件的开头导入Seederuse并且类名应该以大写字母开头。

Now you should run composer dump-autoloadand now when you run php artisan db:seedit will work fine.

现在你应该运行composer dump-autoload,现在当你运行php artisan db:seed它会正常工作。

回答by DannyFeliz

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

只需运行 composer dump-autoload -o自动加载器以获取新类,因为数据库文件夹不会自动使用 PSR-4 自动加载。

回答by rabin

This worked for me

这对我有用

composer dump-autoload -o 

回答by hamidreza samsami

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

我有同样的问题,但您可以通过添加命名空间来解决它:

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

回答by Fernando Kosh

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

$ composer 自我更新

Hope it can help others.

希望它可以帮助其他人。