首页 文章

Laravel 5 Class 'app\Models\Colour'未找到

提问于
浏览
0

到今天早上我的应用程序运行良好,我已经在 app/Models 中创建了没有问题的类 . 我添加了一个名为 Colours 的雄辩类,它包含用于查找表的命名颜色的RGB值,但是代码会产生关于不存在的类的致命错误 .

数据库已正确迁移和播种,没有任何问题 . 但是以下代码行打破了我的应用程序

$c = Colour::query()->findOrFail(2);

现在为什么这让我感到麻烦的是我有另一个类轮子,它在同一个类中早先被称为如下

$wheel = Wheel::query()->findOrFail($data['settings']['wheel']);

此代码按请求运行并返回数据行 . 查看其他类未找到的帖子建议命名空间问题,但是颜色和轮子都在同一文件位置( app\Models ),这是在命名空间标记中定义的 . 我've added the class as a use, added both of these as uses, I'已经在 app/ 创造了一个工匠模型我试过了

composer dump-autoload

再也没有找到运气和课程 . 我对此感到茫然,我还有其他类在其他没有这些问题的表上以相同的方式调用数据库查询,我的Wheel和Color模型的内容是相同的(显然除了类名本身) .

另外我've just noticed that I'在 Wheel.php 内,但与我的应用程序使用的主类不同,Wheel类是一个雄辩的对象,这可以阻止我获取另一个表对象的数据流?我该如何解决这个问题?我本质上是否需要使用我的方法创建另一个类并将我的wheel类保持为一个雄辩的类?

该型号没有控制器,与车轮模型没有直接关系 . 从基本控制器中调用此模型的操作是

public function wheel(Request $request)
{
    $data = $request->session()->get('data');
    $model = new Wheel();
    $wheel = $model->drawWheel($data);
    return view('wheel', ['wheel' => $wheel]);
}

据说,直到我想要颜色名称这个工作正常 . 模型是

<?php

namespace app\Models;

use Illuminate\Database\Eloquent\Model;

class Wheel extends Model
{

    private function drawWheels($im, $data, $wheel)
    {
        $m = 250;
        $r = $wheel['inner_r'];
        $spokes = $data['spokes'];
        $l = $data['format'];
        foreach ($spokes as $a) {
            $oA = cos(deg2rad($l[$a['A']]));
            $aA = sin(deg2rad($l[$a['A']]));
            $xA = $m - ($r * $oA);
            $yA = $m - ($r * $aA);
            $oB = cos(deg2rad($l[$a['B']]));
            $aB = sin(deg2rad($l[$a['B']]));
            $xB = $m - ($r * $oB);
            $yB = $m - ($r * $aB);

            $c = Colour::query()->findOrFail(2);
            //$c = Colour::where('name', '=', strtolower($a['name']))->first();
            //$c = DB::table('colours')->where('name', strtolower($a['name']))->first();

            $color = imagecolorallocate($im, $c['R'], $c['G'], $c['B']);

            imageline($im, $xA, $yA, $xB, $yB, $color);
        }

        return $im;
    }

}

这是我的app / Models / Color类

<?php

namespace app\Models;

use Illuminate\Database\Eloquent\Model;

class Colour extends Model
{
    protected $guarded = ['id'];
}

P.S在基本控制器中引用的drawWheel函数只调用私有函数drawWheels(好吧它做其他调用,但这些工作,我可以使用xdebug逐步执行代码,它一直工作,直到它尝试从数据库加载颜色行)

我得到的完整错误是

FatalErrorException in Wheel.php line 64: Class 'app\Models\Colour' not found

in Wheel.php line 64
at FatalErrorException->__construct('message' => '', 'code' => '', 'severity' => '', 'filename' => '', 'lineno' => '', 'traceOffset' => '', 'traceArgs' => '', 'trace' => '') in HandleExceptions.php line 133
at HandleExceptions->fatalExceptionFromError('error' => '', 'traceOffset' => '') in HandleExceptions.php line 118
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at Wheel->drawAspects('im' => '', 'data' => '', 'wheel' => '') in Wheel.php line 30
at Wheel->drawHoroscope('data' => '') in ChartController.php line 33
at ChartController->wheel('request' => '') in Controller.php line 256
at call_user_func_array('', '') in Controller.php line 256
at Controller->callAction('method' => '', 'parameters' => '') in ControllerDispatcher.php line 164
at ControllerDispatcher->call('instance' => '', 'route' => '', 'method' => '') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}('request' => '') in Pipeline.php line 139
at call_user_func('', '') in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in Pipeline.php line 103
at call_user_func('', '') in Pipeline.php line 103
at Pipeline->then('destination' => '') in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack('instance' => '', 'route' => '', 'request' => '', 'method' => '') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch('route' => '', 'request' => '', 'controller' => '', 'method' => '') in Route.php line 203
at Route->runWithCustomDispatcher('request' => '') in Route.php line 134
at Route->run('request' => '') in Router.php line 712
at Router->Illuminate\Routing\{closure}('request' => '') in Pipeline.php line 139
at call_user_func('', '') in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in Pipeline.php line 103
at call_user_func('', '') in Pipeline.php line 103
at Pipeline->then('destination' => '') in Router.php line 714
at Router->runRouteWithinStack('route' => '', 'request' => '') in Router.php line 679
at Router->dispatchToRoute('request' => '') in Router.php line 639
at Router->dispatch('request' => '') in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}('request' => '') in Pipeline.php line 139
at call_user_func('', '') in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle('request' => '', 'next' => '') in Pipeline.php line 124
at call_user_func_array('', '') in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle('request' => '', 'next' => '') in Pipeline.php line 124
at call_user_func_array('', '') in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in StartSession.php line 62
at StartSession->handle('request' => '', 'next' => '') in Pipeline.php line 124
at call_user_func_array('', '') in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle('request' => '', 'next' => '') in Pipeline.php line 124
at call_user_func_array('', '') in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in EncryptCookies.php line 59
at EncryptCookies->handle('request' => '', 'next' => '') in Pipeline.php line 124
at call_user_func_array('', '') in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle('request' => '', 'next' => '') in Pipeline.php line 124
at call_user_func_array('', '') in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}('passable' => '') in Pipeline.php line 103
at call_user_func('', '') in Pipeline.php line 103
at Pipeline->then('destination' => '') in Kernel.php line 122
at Kernel->sendRequestThroughRouter('request' => '') in Kernel.php line 87
at Kernel->handle('request' => '') in index.php line 54
at {main}() in index.php line 0

第64行的Wheel中的线是

$c = Colour::query()->findOrFail(2);

我的composer.json自动加载部分(和开发者)是

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},

1 回答

  • 3

    模型的命名空间应为 App\Models ,而不是 app\Models .

    您可以在PSR-4自动加载上查看composer documentation .

相关问题