Search code examples
laravellaravel-5laravel-5.3

Model is added in autoload file of Laravel but I facing issue while calling its code


I am new to Laravel I am trying to call a function from my model but facing the following issue

Symfony \ Component \ Debug \ Exception \ FatalThrowableError   (E_ERROR)
Class 'App\Http\Controllers\Role' not found

While model is already loaded by autoloader

'App\\Role' => $baseDir . '/app/Models/Role.php'

My controller has the following code

namespace App\Http\Controllers;

class RoleController extends Controller
{
public function CreateRole()
{
    $owner = new Role();
    $owner->name         = 'owner';
    $owner->display_name = 'Project Owner'; // optional
    $owner->description  = 'User is the owner of a given project'; // optional
    $owner->save();

    $admin = new Role();
    $admin->name         = 'admin';
    $admin->display_name = 'User Administrator'; // optional
    $admin->description  = 'User is allowed to manage and edit other users'; // optional
    $admin->save();
}

}

Thanks,

Aisha Zafar


Solution

  • On the begining of the file try to do this:

    namespace App\Http\Controllers;
    use App\Role;                  // <-- Add this
    
    class RoleController extends Controller
    {
     ...