belongsToMany why is returning just the first?

Posted 8 months ago by hj_junior

I'm trying to create a table who will group a list of permissions to each user, so I have created this following tables:

Table: N_perfilacesso - Group of permissions (pages)

  • _id (primary key)
  • nome (text)

Table: N_paginas - Just list the pages of system

  • _id (primary key)
  • nome (text)

Table: N_perfilacesso_paginas - List of pages of each group of permissions

  • _id (primary key)
  • _perfilacesso (References to N_perfilacesso)
  • _pagina (References to N_paginas)

So now, the models:

PerfilAcesso

    protected $table = 'N_perfilacesso';
    protected $primaryKey = '_id';
    protected $hidden = ['deleted_at','created_at','updated_at','pivot'];
    public function paginas () {
        return $this->belongsToMany('App\Models\PaginasSistema', 'N_perfilacesso_paginas', '_pagina', '_id');
    }

PerfilAcessoPaginas

    protected $table = 'N_perfilacesso_paginas';
    protected $primaryKey = '_id';
    protected $hidden = ['deleted_at','created_at','updated_at','pivot'];

PaginasSistema

    protected $table = 'N_paginas';
    protected $primaryKey = '_id';
    protected $hidden = ['deleted_at','created_at','updated_at','pivot'];
    public function perfilacesso () {
        return $this->belongsToMany('App\Models\PerfilAcesso', 'N_perfilacesso_paginas', '_pagina', '_id');
    }

So in the controller I find the id of profile of access, and after I want to list the pages

    $perfilacesso = PerfilAcesso::findOrFail($id);
    return response([
        'status' => 'ok',
        'pages' => $perfilacesso->paginas
    ]);

But I'm getting result with just the first of result in array

{
    "status": "ok",
    "paginas": [
        {
            "_id": 1,
            "nome": "evadidos.grafico.instituicao"
        }
    ]
}

Update 2

Now I'm trying to debbug better, I found something that I think its stranger, look eloquent query:

        {
            "query": "select [N_paginas].*, [N_perfilacesso_paginas].[_pagina] as [pivot__pagina], [N_perfilacesso_paginas].[_id] as [pivot__id] from [N_paginas] inner join [N_perfilacesso_paginas] on [N_paginas].[_id] = [N_perfilacesso_paginas].[_id] where [N_perfilacesso_paginas].[_pagina] = ? and [N_paginas].[deleted_at] is null",
            "bindings": [
                1
            ],
            "time": 0
        }

As you can see [N_perfilacesso_paginas].[_pagina] = ? its because of this, eloquent its getting just one, but I don't know how to solve it

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.