Error when trying to perform a Relationship belongsToMany in Laravel

Asked

Viewed 179 times

0

I am trying to effect a relationship belongsToMany in my Model but is bringing the error:

Queryexception SQLSTATE[42S02]: Base table or view not found: 1146 Table 'api.codigo' doesn’t exist (SQL: select sequenciaaprovacaos.*, codigo.sequencia as pivot_sequencia, codigo.sequenciaaprovacao_id as pivot_sequenciaaprovacao_id from sequenciaaprovacaos Inner Join codigo on sequenciaaprovacaos.id = codigo.sequenciaaprovacao_id Where codigo.sequencia in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51))

My Models

namespace App;

use Illuminate\Database\Eloquent\Model;


class SequenciaUsuario extends Model
{

    protected $table = 'sequencia_usuarios';

    protected $fillable = [

        'sequencia',
        'recnum',
        'usuario',
        'qtde_aprova',
        'ordem'
    ]; 


    public function grupos()
    {
     return $this->belongsToMany('App\Model\CadSequenciaAprovacao', 'codigo','sequencia');
    }

    public function cadastro()
    {
        return $this->belongsToMany('App\Model\CadUsuario','cod_adm');
    }
}

namespace App;

use Illuminate\Database\Eloquent\Model;


class CadSequenciaaprovacao extends Model
{
    protected $table = 'sequenciaaprovacaos';

    protected $fillable = [
        'codigo',
        'descricao',
    ];

    public function users()
    {
        return $this->hasMany('App\CadSequenciaUsuario','sequencia');
    }

}
  • 1

    $this->belongsToMany('App\Model\CadSequenciaAprovacao', 'codigo','sequencia'); here you are wrong, what is the name of the intermediate table! has how to put the bank diagram?

  • I am without the Diagram here but sequential

  • If you can put

1 answer

0

There are a few options you can test. Since you are not using the default table name (see need to enter the $table attribute), it is interesting that you enter the relationship by passing all parameters accepted by Eloquent to this method.

return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');

in which

  • App\Role coincides with your model;
  • role_user coincides with your table; user_id coincides with its
  • fk in the table and PK in its table that relates;
  • role_id coincides with your fk in the table which is PK in the table with which you want to relate;

For more information, see here the documentation at this point.

I hope I’ve helped.

Browser other questions tagged

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