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

Asked

Viewed 156 times

1

I have several 4 tables Starting with Users -> Departments -> categoria_departamento -> category -> posts; Where the categoria_department table it serves as pivot for relation Many to Many between the departments table and the category table.

inserir a descrição da imagem aqui

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 was found (Class 'Department::class' not found) I’m doing it like this, in the Category.php model:

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

And in the model Departments I did so see:

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

I really don’t know why you’re making this mistake. What I’m doing wrong?

  • 1

    Remove the quotation marks where you have ::class

  • 1

    No vei... I can’t believe I didn’t think it could be the quotes.. kkkkkkkk Caracas man. Straight

  • Because with some you have to have highs and others don’t?

  • I don’t understand Natan the last question!

  • Automatic corrector changed the word. The question is. PQ with some relationships need quotes and others do not?

  • The more you solve the problem.

  • It all depends on how you use it as described in my answer or Department::class or "App Department" which means the same thing ... ! ;) vlw

Show 2 more comments

1 answer

1


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

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

To better illustrate whether your class is done within the namespace App the command Departamento::class returns: App\Departamento (that is to say, the class name and its namespace Correspondente), which is what you need inside the $this->belongsToMany.

Reference: Class name Resolution via ::class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.