BelongsToMany Laravel 5.4 Class 'Department :: class' not found

1

I have several 4 tables Starting with Users - > Departments - > category_department - > category - > postings; Where the department_category table it serves as pivo for many to many relationship between the departments table and the category table.

These are the last 4 tables.

When I enter the Category model and having to do a belongsToMany in the Department model appears a FatalThrowableError saying that my class has been found (Class 'Department: class' not found) I'm doing this in the Category.php template:

public function departamentos()
{
    return $this->belongsToMany('Departamento::class', 'categoria_departamento');
}

And in the Departments model I did so see:

public function categorias()
{
    return $this->belongsToMany('Categoria::class' , 'categoria_departamento');
}

I really do not know why you are giving this error. What am I doing wrong?

    
asked by anonymous 11.09.2017 / 19:06

1 answer

1

The correct one is without the single quotes in this case where Departamento::class s ignores that the fully qualified name of the class will be obtained and it is worth remembering that this command works from 5.5.x version of , example :

public function departamentos()
{
    return $this->belongsToMany(Departamento::class, 'categoria_departamento');
}

To better exemplify if your class is done within namespace App the command Departamento::class returns: App\Departamento ie the class name and its corresponding namespace e), which is what you need within $this->belongsToMany .

Reference: Class name resolution via :: class

    
11.09.2017 / 21:08