Laravel 5 Entrust (Zizaco) error “Class ‘Role’ not found”

Posted on February 24, 2015 in Web Dev

I was having this problem for a good 30 mins until it finally hit me. Something glaringly obvious to any seasoned Laravel dev, or anyone with more than a few weeks experience in Laravel 5 for that matter, but a fairly easy thing to miss for a newbie to the L5 way of doing things.

I was 100% sure I followed the instruction exactly as they were presented. I had no issues with Entrust in Laravel 4.x, but was with Laravel 5. This led me to believe that were was some step(s) required for Entrust in L5 that wasn’t required in L4. That had to be the only reason. The only other potential reason that I could think of was a possibility that the Entrust package wasn’t quite ready, but as it turns out it was just the docs that weren’t necessarily ready.

The problem was that I had not namespaced the Role model.

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.

Leave a comment

Was this helpful? Did I miss something? Do you have a question? Get in touch, or tell me below.