【发布时间】:2015-12-03 11:00:42
【问题描述】:
直到今天早上,我的应用程序运行良好,我在 app/Models 中创建了课程,没有任何问题。我添加了一个名为Colours 的雄辩类,其中包含用于查找表的命名颜色的 RGB 值,但是代码会产生一个关于该类不存在的致命错误。
数据库已正确迁移和播种,没有任何问题。但是以下代码行破坏了我的应用程序
$c = Colour::query()->findOrFail(2);
现在为什么这让我感到困扰是我有另一个类 Wheel,它在同一个类中早先被调用如下
$wheel = Wheel::query()->findOrFail($data['settings']['wheel']);
此代码运行并按要求返回数据行。查看其他未找到类的帖子表明存在命名空间问题,但是颜色和滚轮位于同一文件位置 (app\Models),这是在命名空间标记中定义的。我已将类添加为用途,将这两个都添加为用途,我已经在app/ 中创建了一个带有工匠的模型我已经尝试过
composer dump-autoload
再次没有运气,也没有找到课程。我对此感到茫然,我有其他类在其他没有此类问题的表上以相同的方式调用数据库查询,我的 Wheel 和 Color 模型的内容是相同的(显然类名本身除外)。
另外我刚刚注意到我在Wheel.php 内,但与我的应用程序使用的主类不同,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;
}
}
这是我的应用/模型/颜色类
<?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
Wheel 中第 64 行的行是
$c = Colour::query()->findOrFail(2);
我的 composer.json 自动加载部分(和开发部分)是
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
【问题讨论】:
-
你能展示你的模型和控制器吗? (相关部分)
-
如果没有看到您的文件夹结构、自动加载方案和导入的类,真的很难说是哪里出了问题。
-
确保你的控制器中有
use app\Models\Colour -
@Daan 更新了相关控制器和模型详细信息的帖子。 Tezla 我已经尝试过了,但在应用程序的其他模型中,我没有使用完整的类并且它可以工作,添加它不起作用。 vikingmaster 为了简单起见,我所有的模型类都在 App/Models 中,因为我还在学习 laravel。自动装载机不是自动完成的吗?不确定导入的课程,我怎么知道它们是什么?正如我在最初的帖子中所说,我有另一个模型可以加载其他几个模型,这根本没有问题。今天只加了这个。
-
app\Models\Color 应该位于 app\Models 文件夹中。不需要dum-autoload。
标签: php class oop laravel eloquent