带有 Entrust 的 Laravel 5.2 - 类名必须是有效的对象或字符串

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

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

phplaraveluser-rolesentrust

提问by Jose

I have a problem when register a user, after save user in my table users, I try assign role for this user but I receive the error :

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

Class name must be a valid object or a string.

类名必须是有效的对象或字符串。

my code is

我的代码是

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

(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');

        }
}

the echoon $userprint this:

echo$user打印:

{"name":"bbbbbvq","email":"[email protected]","active":1,"updated_at":"2016-03-03 19:07:24","created_at":"2016-03-03 19:07:24","id":32}

{"name":"bbbbvq","email":"[email protected]","active":1,"updated_at":"2016-03-03 19:07:24","created_at":"2016 -03-03 19:07:24","id":32}

I copied entrust Zizaco\Entrust\src\config\config.phpto my proyect\app\config\entrust.phpand I modified the file test\vendor\zizaco\entrust\src\Entrust\EntrustServiceProvider.phpin this method:

我将委托复制Zizaco\Entrust\src\config\config.php到我的proyect\app\config\entrust.phptest\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();
        });
    }

回答by RobinM

This resolved the issue for me.

这为我解决了这个问题。

vendor/zizaco/entrust/src/Entrust/Traits/EntrustRoleTrait.phpat line 51 makes the call Config::get('auth.model')as the first parameter to the $this->belongsToMany(method call.

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'), ...
}

You can either change this to Config::get('auth.providers.users.model')or update your config/auth.phpfile to include an entry model => App\Users::class

您可以将其更改为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,
    ],
],

My preference is to update the config/auth.phpfile since any changes to the vendor folder will not be available to others on your team, or when you move to production.

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

Of course if you are using a different Model for your Users you would supply that instead.

当然,如果你为你的用户使用不同的模型,你会提供它。

回答by James

Entrust hasn't been upgraded for 5.2 yet so you need to fiddle around with it a little.

Entrust 尚未针对 5.2 进行升级,因此您需要稍微处理一下。

As tapos gosh has said beforeyou need to go in to vendor/zizaco/entrust/src/commands/MigrationCommand.phpand on line 86:

正如tapos gosh您需要进入vendor/zizaco/entrust/src/commands/MigrationCommand.php第86行之前所说的那样

Remove

消除

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

And replace it with

并将其替换为

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

And then in config/auth.phpfile write the provider line like so:

然后在config/auth.php文件中写入 provider 行,如下所示:

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

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

回答by vignesh

I am facing the same problem for assignRole() to the user. Solution is roletable guard_namemust be '

我对用户的assignRole()面临同样的问题。解决办法是角色guard_name必须是'

web

网络

'.

'。