Error trying to make a belongsToMany relationship in Laravel

0

I'm trying to make a belongsToMany relationship in my Model but it's bringing the error:

  

QueryException       SQLSTATE [42S02]: Base table or view not found: 1146 Table 'api.codigo' does not 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,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,49,49,50,50))

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');
    }

}
    
asked by anonymous 11.12.2017 / 21:34

1 answer

0

There are a few options you can try. 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 the parameters accepted by Eloquent to this method.

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

where

  • App\Role matches your model;
  • role_user matches your table; user_id matches your
  • fk in the table and PK in your listing table;
  • role_id matches your fk in the table that is PK in the table you want to relate to;

For more information, see here documentation at this point.

I hope I have helped.

    
13.12.2017 / 15:20