我们有一个模型App\Models\Tag。在我们正在做的帮助类中:
<?php
namespace App\Helpers;
use App\Models\Tag;
class Helper {
/**
* Get tag
*
**/
public static function tag($path){
return Tag::where('path', '=', $path)->first();
}
}这会产生错误:
FatalErrorException in Model.php line 780:
Class 'Tag' not found即使使用App\Models\Tag::where('path', '=', $path)->first()也会产生错误:
FatalErrorException in Helper.php line 15:
Class 'App\Helpers\App\Models\Tag' not found最奇怪的是,我们可以在没有问题的情况下使用来自控制器的App\Models\Tag。因此,问题似乎是在这个助手类上,而不是模型上。有什么想法吗?
Tag.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
// database table
protected $table = 'tags';
}
?>编辑1
Tag.php模型是手动添加的,而不是使用artisan。所以,它有可能在类图中丢失。在https://stackoverflow.com/a/35142715/1008916上的建议中,我们尝试了composer dump-autoload -o,但这并没有帮助。
编辑2
我们最终将这个问题追溯到了一个与Tag.php相关的不同模型。引起问题的代码行是:
$tag = Tag::where('path', '=', $path)->first();
if ($tag == null) {
$tagLet = TagLet::where('path', '=', $path)->first();
if ($tagLet != null) {
$tag = $tagLet->tag;
}
}当模型抛出错误时,Laravel没有给出发生错误的行号,因此我们将标签的第一次出现作为原因,而真正的原因是最后一次发生($tag = $tagLet->tag;)。
TagLet.php有:
public function tag()
{
return $this->belongsTo('Tag');
}正如@Rodrane & @SauminiNavaratnam所建议的,解决方案是使用'App\Models\Tag‘而不是'Tag’
public function tag()
{
return $this->belongsTo('App\Models\Tag');
}发布于 2017-05-12 08:33:55
检查您的Tag.php文件的位置,我认为它在App目录中,如果名称从App\Model更改为app。如果不检查文件夹结构和名称,请检查名称。
https://stackoverflow.com/questions/43928220
复制相似问题