phplaraveluser-rolesentrust

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


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":"bb@bbv.comq","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();
        });
    }

Solution

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

    As tapos gosh has said before you need to go in to vendor/zizaco/entrust/src/commands/MigrationCommand.php and on line 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.php file write the provider line like so:

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