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)
<?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 echo
on $user
print this:
{"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}
I copied entrust Zizaco\Entrust\src\config\config.php
to my proyect\app\config\entrust.php
and I modified the file test\vendor\zizaco\entrust\src\Entrust\EntrustServiceProvider.php
in this method:
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();
});
}
This resolved the issue for me.
vendor/zizaco/entrust/src/Entrust/Traits/EntrustRoleTrait.php
at line 51 makes the call Config::get('auth.model')
as the first parameter to the $this->belongsToMany(
method call.
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.php
file to include an entry model => App\Users::class
'model' => App\Users::class,
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Users::class,
],
],
My preference is to update the config/auth.php
file since any changes to the vendor folder will not be available to others on your team, or when you move to production.
Of course if you are using a different Model for your Users you would supply that instead.