hj_junior

Experience

80

0 Best Reply Awards

  • Member Since 3 Months Ago
  • 0 Lessons Completed
  • 0 Favorites

19th June, 2018

hj_junior left a reply on BelongsToMany Why Is Returning Just The First? • 3 months ago

I got it do, but I have a lot of changes, I tried to keep everything in the standart of laravel, so know I'm not using prefix and nothing else, so the breaking changes:

Migrations

    public function basetable() {
        $this->integer('id')->autoIncrement()->comment("Identificação");
        $this->timestamp('created_at')->useCurrent()->comment("timestamp de criação");
        $this->timestamp('updated_at')->comment("timestamp de atualização")->nullable();
        $this->timestamp('deleted_at')->nullable()->comment("timestamp de exclusão")->nullble();
    }
´´´


$this->schema->create('perfilacessos', function (CustomBlueprint $table) {
    $table->basetable();
    $table->text('nome');
});

    $this->schema->create('paginas', function (CustomBlueprint $table) {
        $table->basetable();
        $table->text('nome');
    });

    $this->schema->create('pagina_perfilacessos', function (CustomBlueprint $table) {
        $table->basetable();
        $table->integer('perfilacessos_id');
        $table->integer('pagina_id');
        $table->foreign('perfilacessos_id')->references('id')->on('perfilacessos');
        $table->foreign('pagina_id')->references('id')->on('paginas');
    });

´´´

Models

PerfilAcesso -> Perfilacessos (Renamed)

    public function paginas () {
        return $this->belongsToMany(Pagina::class);
    }
´´´
#### PaginasSistema -> Pagina (Renamed)
public function perfilacesso () {
    return $this->belongsToMany(Perfilacessos::class);
}

´´´

18th June, 2018

hj_junior left a reply on BelongsToMany Why Is Returning Just The First? • 3 months ago

@Snapey I think shoud be belongsToMany because of this, look this ER diagram

(https://i.imgur.com/X9q3dhg.png)

hj_junior left a reply on BelongsToMany Why Is Returning Just The First? • 3 months ago

@Snapey Yes, sorry, I've added this information in the blocks of code too and some stranger think that I have see it in the query.

hj_junior started a new conversation BelongsToMany Why Is Returning Just The First? • 3 months ago

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)

| Column | Details | | ------------- | ------------- | | _id | primary key | | nome | text |

Table: N_paginas Just list the pages of system

| Column | Details | | ------------- | ------------- | | _id | primary key | | nome | text |

Table: N_perfilacesso_paginas List of pages of each group of permissions

| Column | Details | | ------------- | ------------- | | _id | primary key | | _perfilacesso | References to N_perfilacesso | | _pagina | References to N_paginas |

So now, the models:

PerfilAcesso

    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 $hidden = ['deleted_at','created_at','updated_at','pivot'];

PaginasSistema

    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"
        }
    ]
}
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.