Laravel FatalErrorException in Model.php line 827: class not found

I am very new to Laravel. I tried using one example in my code, but as a result I got an error - FatalErrorException in Model.php line 827: Class Category not found.

Subsequently, I changed one line of code and fixed the error. However, I really do not understand the cause of the error and how I fixed it.

This is my code (as a result, when I try to build a query using category- I get an error):

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Category;

class Translation extends Model
{
    protected $fillable = array('lang1', 'lang2', 'lang1_code', 'lang2_code', 'category_id', 'created_at', 'updated_at');

    public function category() {
        return $this->belongsTo('Category', 'category_id');
        //return $this->belongsTo('App\Category', 'category_id');
    }        
}

However, when I change one line, I do not get the above error:

old line (error): return $this->belongsTo('Category', 'category_id');

new line (no error): return $this->belongsTo('App\Category', 'category_id');

+4
source share
2 answers

All your models are usually stored in the application folder.

, ,

App\Category
+2

PHP 5.5:

'App\Category'

PHP 5.5 class . , ClassName, ClassName::class. .

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Category;

class Translation extends Model
{
    protected $fillable = array('lang1', 'lang2', 'lang1_code', 'lang2_code', 'category_id', 'created_at', 'updated_at');

    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }        
}
+2

Source: https://habr.com/ru/post/1613511/


All Articles