0

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

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

我的代码是

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

        }
}

在打印这个echo$user

{"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}

我将委托复制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();
        });
    }
4

3 回答 3

7

这为我解决了这个问题。

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文件,因为对供应商文件夹的任何更改都不会提供给您团队中的其他人,或者当您转移到生产环境时。

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

于 2016-04-13T05:21:01.543 回答
3

Entrust 还没有升级到 5.2,所以你需要稍微摆弄一下。

正如 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',
    // ],
],
于 2016-03-04T00:29:06.837 回答
0

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

网络

'。

于 2019-05-09T10:30:44.113 回答