php Laravel 迁移:“未找到”类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30819934/
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:05:26  来源:igfitidea点击:

Laravel migrations: Class "not found"

phpsymfonylaravelazure

提问by Washery

I am deploying a Laravel barebone project to Microsoft Azure, but whenever I try to execute php artisan migrateI get the error:

我正在将 Laravel 准系统项目部署到 Microsoft Azure,但是每当我尝试执行时,php artisan migrate我都会收到错误消息:

[2015-06-13 14:34:05] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class '' not found' in D:\home\site\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:328

[2015-06-13 14:34:05] production.ERROR: 异常 'Symfony\Component\Debug\Exception\FatalErrorException',在 D:\home\site\vendor\laravel\framework 中出现消息 'Class'' not found' \src\Illuminate\Database\Migrations\Migrator.php:328

Stack trace:

堆栈跟踪:

 #0 {main}  

What could be the problem? Thank you very much

可能是什么问题呢?非常感谢

-- edit --

- 编辑 -

Migration Class

迁移类

<?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->bigIncrements('id');
            $table->string('name', 50);
            $table->string('surname', 50);
            $table->bigInteger('telephone');
            $table->string('email', 50)->unique();
            $table->string('username', 50)->unique();
            $table->string('password', 50);
            $table->boolean('active')->default(FALSE);
            $table->string('email_confirmation_code', 6);
            $table->enum('notify', ['y', 'n'])->default('y');
            $table->rememberToken();
            $table->timestamps();

            $table->index('username');
        });
    }

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

回答by Umair Ahmed

For PSR-4 Auto Loader Users (composer.json):

对于 PSR-4 自动加载器用户 (composer.json):

Keep the migrations folder inside classmaparray and do not include it inside psr-4 object under autoloadhead. As migrations' main class Migrator doesn't support namespacing. For example;

将迁移文件夹保留在classmap数组中,不要将其包含在autoload头部下的 psr-4 对象中。由于迁移的主类 Migrator 不支持命名空间。例如;

"autoload": {
    "classmap": [
        "app/database/migrations"
    ],
    "psr-4": {
        "Acme\controllers\": "app/controllers"
    }
}

Then run:

然后运行:

php artisan clear-compiled 
php artisan optimize:clear
composer dump-autoload
php artisan optimize
  • First one clears all compiled autoload files.
  • Second clears Laravel caches (optional)
  • Third builds the autoloader for namespaced classes.
  • Fourth optimizes various parts of your Laravel app and builds the autoloader for non-namespaced classes.
  • 第一个清除所有编译的自动加载文件。
  • 秒清除 Laravel 缓存(可选)
  • 第三个为命名空间类构建自动加载器。
  • 第四优化了 Laravel 应用程序的各个部分,并为非命名空间类构建自动加载器。

From this time on wards, you will not have to do this again and any new migrations will work correctly.

从此时起,您将不必再次执行此操作,任何新的迁移都将正常工作。

回答by Mohamed Kawsara

Simply make sure your migration filename is the same as your class name.

只需确保您的迁移文件名与您的类名相同。

i.e:

IE:

If filename is:

如果文件名是:

xxx_151955_create_post_translations_table.php

xxx_151955_create_post_translations_table.php

Then class should be:

那么类应该是:

CreatePostTranslationsTable

CreatePostTranslationsTable

回答by Jambor - MSFT

If you get the "Class not found error" when running migrations, please try running this command.

如果在运行迁移时遇到“找不到类错误”,请尝试运行此命令。

composer dump-autoload 

then re-issuing the migrate command. See more details in the offical site (#Running Migrations): http://laravel.com/docs/master/migrations#running-migrations

然后重新发出迁移命令。在官方网站(#Running Migrations)中查看更多详细信息:http://laravel.com/docs/master/migrations#running-migrations

回答by jacurtis

I had this same problem a while back. Apparently it is a common problem because in the documentation for Laravel, it even suggests it: http://laravel.com/docs/master/migrations#running-migrations

不久前我遇到了同样的问题。显然这是一个常见问题,因为在 Laravel 的文档中,它甚至建议它:http://laravel.com/docs/master/migrations#running-migrations

Basically all you have to do is refresh some composer files. Simply run:

基本上你所要做的就是刷新一些作曲家文件。只需运行:

composer dump-autoload

composer dump-autoload

This will refresh composer autoload files and then you can run your normal migration and it should work! Very best.

这将刷新作曲家自动加载文件,然后您可以运行正常的迁移,它应该可以工作!非常好。

回答by Al Masum Fahim

i also run in the same problem.

我也遇到同样的问题。

The solution for me was to delete the migration file, AND delete the record from the "migrations" table in the database.

我的解决方案是删除迁移文件,并从数据库中的“迁移”表中删除记录。

After that, I ran

在那之后,我跑了

composer dump-autoload

作曲家转储自动加载

and was finally able to reset/rollback migrations.

并最终能够重置/回滚迁移。

回答by Behnam Azimi

I think it's late to answer this question but maybe this will help someone.

我认为回答这个问题为时已晚,但也许这会对某人有所帮助。

If you changed the migration file name, be sure about its inner class name.

如果您更改了迁移文件名,请确定其内部类名。

For example, If I change a migration name from 2018_06_10_079999_create_admins_table.phpto 2018_06_10_079999_create_managers_table.phpso its inner class name must change from CreateAdminsTableto CreateManagerTabletoo.

例如,如果我将迁移名称从2018_06_10_079999_create_admins_table.phpto更改为,2018_06_10_079999_create_managers_table.php那么它的内部类名称也必须从CreateAdminsTableto更改CreateManagerTable

回答by m2j

I deleted one of the migration file. faced the same problem, while php artisan migrate:rollback

我删除了其中一个迁移文件。面临同样的问题,而php artisan migrate:rollback

Then I tried composer dump-autoload. Again the same turned up.

然后我尝试了composer dump-autoload。再次出现同样的情况。

I restored the deleted file and tried composer dump-autoloadand php artisan migrate:rollback. It works.

我恢复被删除的文件,并试图composer dump-autoloadphp artisan migrate:rollback。有用。

回答by Matthew Park

For me, the problem was that I had named my migration 2017_12_15_012645_create_modules_problems.php, with the class name of CreateModulesProblemsTable. As soon as I added _table to the filename, everything worked fine.

对我来说,问题是我将迁移命名为 2017_12_15_012645_create_modules_problems.php,类名为 CreateModulesProblemsTable。我将 _table 添加到文件名后,一切正常。

回答by mediaguru

I had a similar situation (class not found error) after moving a Laravel 5.2 dev project to production. The production server was looking for class "project" but the controller name was Project.php. Once I renamed the file to project.php it was good to go.

将 Laravel 5.2 开发项目移至生产环境后,我遇到了类似的情况(未找到类错误)。生产服务器正在寻找类“project”,但控制器名称是 Project.php。一旦我将文件重命名为 project.php 就可以了。

回答by stilo bit

i face this problem when i rename migrations to :

当我将迁移重命名为:

0_create_activities_table.php

0_create_activities_table.php

i solve it when i rename it to:

当我将其重命名为:

2019_10_01_0_create_activities_table.php

2019_10_01_0_create_activities_table.php