未找到模型。 Laravel 框架中的错误?

Model is not found. Bug in Laravel Framework?

我的模型是这样的

class redModel extends Model {

    public $table = 'tbl50red';
    public $primaryKey = 'RedID';
    public $timestamps = true;

}

这是另一个模型

class AddOnModel extends Model {

    public $table = 'tbladdon';
    public $primaryKey = 'AddOnID';
    public $timestamps = true;

    public function AppRed() {
        return $this->hasMany("\App\Models\AddOn\Red\redModel", "AddOnID", "AddOnID");
    }
}

当我在控制器中 运行 这个:

$AddOns = AddOnModel
    ::with(array("AppRed" => function($query) use($StartDate, $EndDate) {
        return $query->whereBetween("EarningDate", array($StartDate, $EndDate));
    }))
    ->get();

我得到一个例外:

Class '\App\Models\AddOn\Red edModel' not found

我是不是漏掉了什么?

当我打开以下文件时:

\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php

并检查了这个功能

public function hasMany($related, $foreignKey = null, $localKey = null)
{
    $foreignKey = $foreignKey ?: $this->getForeignKey();
    $instance = new $related;

    $localKey = $localKey ?: $this->getKeyName();

    return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
}

我看到我无法实例化模型,因为模型名称以 r 开头。哈哈

我从早上开始就在拉头发,另一个巧合是它是这样的 \r 所以它缺少型号名称,我认为用空白 space 替换 \r 并且由于 url变成这样

\App\Models\AddOn\Red edModel

实际是这样的

\App\Models\AddOn\Red\redModel

我认为这是 Laravel 框架中的一个错误。

"\App\Models\AddOn\Red\redModel"更改为:

"\App\Models\AddOn\Red\redModel"

即额外的 \ 将转义 \r\r 是一个控制字符,这就是您遇到问题的原因。

这不是 Laravel 中的错误。请阅读 PSR-1 Basic Coding Standard ,这将使您以后可以轻松避免此错误。即,这是您违反的部分:

Class names MUST be declared in StudlyCaps.

你可以试试给定的方法...

use Illuminate\Database\Eloquent\Model;

class Application extends Model
{
    public function AppRed()
    {
        return $this->hasMany(redModel::class);
    }
}