entrust: Class '\Role' not found in laravel 5

i have a problem as following:

  • composer.json “zizaco/entrust”: “dev-laravel-5”,
  • i was run migration & can create roles, permission
  • in model User, i made as guide from author:<?php

use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Eloquent { use EntrustUserTrait; // add this trait to your user model

...

}

then i attach role for an user: $user->attachRole($admin); // parameter can be an Role object, array, or id or $user->hasRole(‘admin’);

=> i got an exception:

PHP Fatal error: Class ‘\Role’ not found in /Volumes/Data/IdooSources/ISAdminTools/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 971 PHP Stack trace: PHP 1. {main}() /Volumes/Data/IdooSources/ISAdminTools/artisan:0 PHP 2. Illuminate\Foundation\Console\Kernel->handle() /Volumes/Data/IdooSources/ISAdminTools/artisan:36 PHP 3. Symfony\Component\Console\Application->run() /Volumes/Data/IdooSources/ISAdminTools/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:91 PHP 4. Symfony\Component\Console\Application->doRun() /Volumes/Data/IdooSources/ISAdminTools/vendor/symfony/console/Symfony/Component/Console/Application.php:126 PHP 5. Symfony\Component\Console\Application->doRunCommand() /Volumes/Data/IdooSources/ISAdminTools/vendor/symfony/console/Symfony/Component/Console/Application.php:195

anyone can help me.

thank in advance!

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20

Most upvoted comments

I am using Laravel 5.2 and have the same problem, that is FatalThrowableError in Model.php line 956: Class ‘App\Role’ not found I just put a name space at the top of Role Class namespace App;

now it works.

[Fix suggested at bottom of this post]

Hi. I had the same problem as @callen5914. I was 100% sure I’ve followed the instruction exactly as they are presented. I had no issues with Entrust in Laravel 4.x, but am with Laravel 5. This led me to believe that there was some step(s) required for Entrust in L5 that wasn’t required in L4.

In my controller I had for example:

<?php namespace App\Http\Controllers;
use App\Models\Role;
class WelcomeController extends Controller {
    public function index()
    {
        $owner = new Role();
        $owner->name         = 'superadmin';
        $owner->display_name = 'Super Admin'; // optional
        $owner->description  = 'The highest level of admin-ness'; // optional
        $owner->save();
    }
}

All of my models were setup exactly as required in the usage instruction docs.

The problem was that my Role.php model (in my case located at /app/models/role.php) did not have a namespace declaration.

Fix

Make sure that your Role.php model looks like this, if for example you’ve moved your models into the /App/Models directory:

<?php namespace App\Models;
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole
{

}

Hope that helps.

Just had the same issue

In entrust.php in config folder change line 22

from ‘role’ => ‘\Role’,

to ‘role’ => ‘App\Role’