Laravel 5.2 with Entrust - Class 名称必须是有效的对象或字符串

Laravel 5.2 with Entrust - Class name must be a valid object or a string

我在注册用户时遇到问题,在我的 table 用户中保存用户后,我尝试为该用户分配角色但我收到错误:

Class name must be a valid object or a string.

我的密码是

(App\Http\Controllers\auth\AuthController.php)

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Role;

use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Controller\Auth;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;


class AuthController extends Controller
{

    public function postRegister(Request $request){

            $this->validate($request,[
                'name' => 'required|min:4|max:255|unique:users',
                'email'=>'required|email|max:255|unique:users',
                'password'=>'required|confirmed|min:3'
                ]);

            $user_data = array(
               //$field => $request->input('login'),
               'name'=> $request->input('name'),
               'email' => $request->input('email'),
               'password' => $request->input('password')
            );

            $user=User::create([
                    'name'=>$user_data['name'],
                    'email'=>$user_data['email'],
                    'password'=>bcrypt($user_data['password']),
                    'active'=>1                
                ]);

            echo $user;
            $role = Role::where('name','=','admin')->first();

            //$user->attachRole($role->id);            
            $user->roles()->attach($role->id);

            //return redirect('auth/register')->with('message','store');

        }
}

$user 上的 echo 打印此:

{"name":"bbbbbvq","email":"bb@bbv.comq","active":1,"updated_at":"2016-03-03 19:07:24","created_at":"2016-03-03 19:07:24","id":32}

我复制了 entrust Zizaco\Entrust\src\config\config.php 到我的 proyect\app\config\entrust.php 并且我用这个方法修改了文件 test\vendor\zizaco\entrust\src\Entrust\EntrustServiceProvider.php:

  private function registerCommands()
    {
        /*
        $this->app->bindShared('command.entrust.migration', function ($app) {
            return new MigrationCommand();
        });
        $this->app->bindShared('command.entrust.classes', function ($app) {
            return new ClassCreatorCommand();
        });*/

        $this->app->singleton('command.entrust.migration', function ($app) {
            return new MigrationCommand();
        });
        $this->app->singleton('command.entrust.classes', function ($app) {
            return new ClassCreatorCommand();
        });
    }

Entrust 尚未升级到 5.2,因此您需要 fiddle 稍微了解一下。

作为 tapos gosh 你需要进入 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',
    // ],
],

这解决了我的问题。

vendor/zizaco/entrust/src/Entrust/Traits/EntrustRoleTrait.php 在第 51 行将调用 Config::get('auth.model') 作为 $this->belongsToMany( 方法调用的第一个参数。

public function users()
{
    return $this->belongsToMany(Config::get('auth.model'), ...
    // return $this->belongsToMany(Config::get('auth.model'), ...
}

您可以将其更改为 Config::get('auth.providers.users.model') 或更新您的 config/auth.php 文件以包含条目 model => App\Users::class

'model' => App\Users::class,  

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

我的偏好是更新 config/auth.php 文件,因为对 vendor 文件夹所做的任何更改都不会对您团队中的其他人可用,或者当您转到生产环境时。

当然,如果您为您的用户使用不同的模型,您将改为提供该模型。

对于 assignRole() 用户,我面临同样的问题。解决方法是 role table guard_name must be '

web

'.