[英]Laravel migrations: Class “not found”
I am deploying a Laravel barebone project to Microsoft Azure, but whenever I try to execute php artisan migrate
I 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
非常感谢
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');
}
}
For PSR-4 Auto Loader Users (composer.json):对于 PSR-4 自动加载器用户 (composer.json):
Keep the migrations folder inside classmap
array and do not include it inside psr-4 object under autoload
head.将迁移文件夹保留在
classmap
数组中,不要将其包含在autoload
头下的classmap
-4 对象中。 As migrations' main class Migrator doesn't support namespacing.由于迁移的主类 Migrator 不支持命名空间。 For example;
例如;
"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
From this time on wards, you will not have to do this again and any new migrations will work correctly.从此时起,您将不必再次执行此操作,任何新的迁移都将正常工作。
Simply make sure your migration filename is the same as your class name.只需确保您的迁移文件名与您的类名相同。
ie: IE:
If filename is:如果文件名是:
xxx_151955_create_post_translations_table.php
Then class should be:那么类应该是:
CreatePostTranslationsTable
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
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
This will refresh composer autoload files and then you can run your normal migration and it should work!这将刷新作曲家自动加载文件,然后您可以运行正常的迁移,它应该可以工作! Very best.
非常好。
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.php
to 2018_06_10_079999_create_managers_table.php
so its inner class name must change from CreateAdminsTable
to CreateManagerTable
too.例如,如果我将迁移名称从
2018_06_10_079999_create_admins_table.php
为2018_06_10_079999_create_managers_table.php
,则其内部类名称也必须从CreateAdminsTable
更改为CreateManagerTable
。
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.并最终能够重置/回滚迁移。
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
.然后我尝试了
composer dump-autoload
。 Again the same turned up.再次出现同样的情况。
I restored the deleted file and tried composer dump-autoload
and php artisan migrate:rollback
.我恢复了删除的文件并尝试了
composer dump-autoload
和php artisan migrate:rollback
。 It works.有用。
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.对我来说,问题是我将迁移命名为 2017_12_15_012645_create_modules_problems.php,类名为 CreateModulesProblemsTable。 As soon as I added _table to the filename, everything worked fine.
我将 _table 添加到文件名后,一切正常。
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
Make sure your migrations filename matches with migration class name确保您的迁移文件名与迁移类名匹配
FOR EXAMPLE:例如:
If name of migration is:如果迁移的名称是:
2020_10_31_161839_create_notifications_table 2020_10_31_161839_create_notifications_table
Then the class name should be:那么类名应该是:
CreateNotificationsTable创建通知表
I had a similar situation (class not found error) after moving a Laravel 5.2 dev project to production.将 Laravel 5.2 开发项目移至生产环境后,我遇到了类似的情况(未找到类错误)。 The production server was looking for class "project" but the controller name was Project.php.
生产服务器正在寻找类“project”,但控制器名称是 Project.php。 Once I renamed the file to project.php it was good to go.
一旦我将文件重命名为 project.php 就可以了。
I was receiving the same class not found error when trying to migrate my project.尝试迁移我的项目时,我收到了相同的类未找到错误。 Sometimes it is the simple things that get you.
有时,是简单的事情让您受益。 In my case, I noticed that my class name was not correct in my migration file due to me making a rename change early on and not carrying that change throughout.
就我而言,我注意到我的迁移文件中的类名不正确,因为我很早就进行了重命名更改并且没有始终进行该更改。
After correcting the class name I performed a composer dump-autoload and my issue went away.更正类名后,我执行了 Composer dump-autoload,我的问题就消失了。
HTH someone :] HTH某人:]
simply delete the row at your database on table migrations
and that will fix the problem.只需在表
migrations
删除数据库中的行即可解决问题。 It will not show anymore when you do migrations当您进行迁移时它不会再显示
other way is to simple create the file it depends of what u want, in my case I wanted to get rid of this migration.另一种方法是简单地创建文件,这取决于你想要什么,在我的情况下,我想摆脱这种迁移。 :)
:)
I had foolishly put:我曾愚蠢地提出:
namespace database\migrations;
Inside my migration create_users_table.php [2014_10_12_000000_create_users_table.php]在我的迁移中 create_users_table.php [2014_10_12_000000_create_users_table.php]
I was getting a similar error - Class 'CreateUsersTable' not found.我遇到了类似的错误 - 找不到类“CreateUsersTable”。
Removing this line at the top solved this error.删除顶部的这一行解决了这个错误。
Just make sure the following two files contain the correct class name and migration name :只需确保以下两个文件包含正确的类名和迁移名称:
C:\\xampp\\htdocs\\StuffSpot\\vendor\\composer\\autoload_classmap.php C:\\xampp\\htdocs\\StuffSpot\\vendor\\composer\\autoload_static.php C:\\xampp\\htdocs\\StuffSpot\\vendor\\composer\\autoload_classmap.php C:\\xampp\\htdocs\\StuffSpot\\vendor\\composer\\autoload_static.php
当我重命名一个迁移文件并解决了这个问题时,我遇到了一种情况,只是在迁移表中我也重写了记录并且一切正常。
In my case was the auto-increment of the database, In the past I did remove manually one entry, and AUTO_INCREMENT
was pointing one more than the next id in the table.在我的情况下是数据库的自动增量,过去我确实手动删除了一个条目,并且
AUTO_INCREMENT
指向的比表中的下一个 id 多。
Apparently Laravel uses AUTO_INCREMENT
-1 to know which was the last migration done.显然 Laravel 使用
AUTO_INCREMENT
-1 来知道哪个是最后一次迁移完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.