我的模型就像这样
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
我错过了什么吗?
答案 0 :(得分:2)
你可以尝试给定的方式......
use Illuminate\Database\Eloquent\Model;
class Application extends Model
{
public function AppRed()
{
return $this->hasMany(redModel::class);
}
}
答案 1 :(得分:0)
当我打开下面的文件时:
\厂商\ laravel \框架\ SRC \照亮\数据库\锋\ 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 \ n它错过了模型名称,我想用空格替换\ r \ n,因为网址变得像这样
\App\Models\AddOn\Red edModel
实际是这个
\App\Models\AddOn\Red\redModel
我认为这是Laravel框架中的一个错误。
答案 2 :(得分:0)
将"\App\Models\AddOn\Red\redModel"
更改为:
"\App\Models\AddOn\Red\\redModel"
即。额外的\
将逃脱\r
。 \r
是一个控制角色,这就是您遇到问题的原因。
答案 3 :(得分:0)
这不是Laravel中的错误。请阅读PSR-1 Basic Coding Standard,以便日后轻松避免此错误。也就是说,这是你违反的部分:
类名必须在
StudlyCaps
中声明。