注册用户时遇到问题,在我的表用户中保存用户后,我尝试为该用户分配角色,但我收到错误:
类名必须是有效的对象或字符串。
我的代码是
(应用\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();
});
}