模型已添加到 Laravel 的自动加载文件中,但我在调用其代码时遇到问题

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

我是新手 Laravel 我正在尝试从我的模型中调用一个函数,但遇到以下问题

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

虽然自动加载器已经加载了模型

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

我的控制器有以下代码

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();
}

}

谢谢,

艾莎扎法尔

在文件的开头尝试这样做:

namespace App\Http\Controllers;
use App\Role;                  // <-- Add this

class RoleController extends Controller
{
 ...

确保模型 class 具有此命名空间:

namespace App;

然后 运行 composer du 命令并使用完整的命名空间:

$owner = new \App\Role;

或在控制器顶部添加use

use App\Role;