如何在 Laravel 5.2 中修复 zizaco entrust:migration 类名验证问题?

12
我按照GitHub上zizac/entrust的安装教程 Link 安装,但遇到了以下错误:

在var/www/html/laravel_test/vendor/zizaco/entrust/src/commands/MigrationCommand.php的第86行,类名必须是一个有效的对象或字符串。

MigrationCommand.php文件的URL为:Link 输出结果:
php artisan entrust:migration

Tables: roles, role_user, permissions, permission_role
A migration that creates 'roles', 'role_user', 'permissions', 'permission_role' tables will be created in database/migrations directory

Proceed with the migration creation? [Yes|no] (yes/no) [yes]: yes

Creating migration...
PHP Fatal error:  Class name must be a valid object or a string in /var/www/html/laravel_test/vendor/zizaco/entrust/src/commands/MigrationCommand.php on line 86

命令:php artisan vendor:publish 成功。

文件:config/entrust.php 存在。

我没有更改 config/auth.php 文件与 auth.php 相同的选项。如何修复?

4个回答

40

在 vendor/zizaco/entrust/src/commands/MigrationCommand.php 的第86行

移除该行:

    $usersTable  = Config::get('auth.table');
    $userModel   = Config::get('auth.model');

添加一行:

$usersTable  = Config::get('auth.providers.users.table');
$userModel   = Config::get('auth.providers.users.model');

并在config/auth.php文件中编写提供程序行,就像我一样:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table' => 'users',
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

那么你的问题将会得到解决: 开心编码


这解决了问题。我使用的是Laravel 5.2并且使用多重认证。这个功能非常好!谢谢。 - Nizam
非常感谢您的回答。它在laravel 5.2中运行良好。这应该被标记为正确答案.. :-) - Vikas Sharma
@taposghosh,在role->delete()操作上出现了相同的错误。我还需要在哪里做出改变? - Aarohi Kulkarni
3
在未来的更新中,直接编辑供应商文件会被覆盖,因此您不应该这样做。相反,您应该将正确的代码行添加到配置文件中,直到Entrust更新其代码库以使用较新的Laravel身份验证布局。 - Brad Bird
你救了我,兄弟。 - Soul Coder
在 Laravel 5.4 中运行......关注 @BradBird。添加到 code config/auth.phpcodecode/* * ENTRUST:MIGRATION 的配置 * * */'table' => 'users', 'model' => App\Models\Usuario::classcode - Cocuba

5

在vendor/zizaco/entrust/src/commands/MigrationCommand.php的第86行。

Laravel 5.1.*新增一行代码。

$usersTable  = Config::get('auth.table');
$userModel   = Config::get('auth.model');

Laravel 5.2.* 添加行

$usersTable  = Config::get('auth.providers.users.table');
$userModel   = Config::get('auth.providers.users.model');

1
虽然这段代码可能回答了问题,但最好包含一些上下文,解释它的工作原理和使用时机。仅有代码的答案从长远来看是没有用处的。 - Bono

3

接受的答案可能会解决问题,但直接编辑供应商文件是非常不好的做法。以下操作可以解决你可能遇到的问题,并且如果你决定更新Entrust并修复其代码库,则仍支持你的应用程序正常工作。

config/auth.php下面添加以下行:

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

Laravel 5.1 - 5.4

'model' => \App\Models\User::class,
'table' => 'users',

一旦Entrust推出更新,您可以选择删除它或保留它。由您决定。

2

尝试运行:

php artisan config:cache

为确保您的应用程序使用最新的配置文件

编辑

好的,现在我明白了,这个库想要使用:

  $usersTable  = Config::get('auth.table');
  $userModel   = Config::get('auth.model');

但是现在的 auth 中已经没有这样的东西了。

所以作为临时解决方案,您应该将 tablemodel 添加到 auth 文件中,如下所示:https://github.com/laravel/laravel/blob/5.1/config/auth.php

等待 Entrust 升级以删除此内容。


谢谢,问题还是一样。我认为问题出在config/auth.php文件中。 - Noproblem
你能展示一下你的 User.php 文件吗?它应该在 App 命名空间中。 - Marcin Nabiałek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接