We have a model App\Models\Tag
. In a helper class we're doing:
<?php
namespace App\Helpers;
use App\Models\Tag;
class Helper {
/**
* Get tag
*
**/
public static function tag($path){
return Tag::where('path', '=', $path)->first();
}
}
This gives error:
FatalErrorException in Model.php line 780:
Class 'Tag' not found
Even using App\Models\Tag::where('path', '=', $path)->first()
gives an error:
FatalErrorException in Helper.php line 15:
Class 'App\Helpers\App\Models\Tag' not found
What's most strange is that we can use App\Models\Tag
from controller without a problem. So, problem seems to be on this helper class and not the model. Any idea?
Tag.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
// database table
protected $table = 'tags';
}
?>
Edit 1
The model Tag.php was manually added instead of using artisan
. So, there's a chance it's missing from classes map. From the suggestion on https://stackoverflow.com/a/35142715/1008916 we tried composer dump-autoload -o
but that didn't help.
Edit 2
We eventually traced the issue to a different model related to Tag.php. The lines of code causing the problem were:
$tag = Tag::where('path', '=', $path)->first();
if ($tag == null) {
$tagLet = TagLet::where('path', '=', $path)->first();
if ($tagLet != null) {
$tag = $tagLet->tag;
}
}
When a model throws an error Laravel does not give line number where the error occurred, so we identified the 1st occurrence of Tag as cause while the real cause was last occurrence ($tag = $tagLet->tag;
).
TagLet.php had:
public function tag()
{
return $this->belongsTo('Tag');
}
As suggested by @Rodrane & @SauminiNavaratnam the solutions is to use 'App\Models\Tag' instead of 'Tag'
public function tag()
{
return $this->belongsTo('App\Models\Tag');
}