dimanche 3 octobre 2021

relationship laravel belongsToMany user roles

i´m traying to create relationship belongsToMany with user role and pivot table assigned_roles.

i have this relation in AssignedRole model

/**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
     public function user()
     {
        return $this->belongsToMany('App\User');
     }

     /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function roleName()
    {
        return $this->belongsTo('App\Role', 'id', 'role_id');
    }

and in my model User:

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function roles()
{
    return $this->belongsToMany('App\AssignedRole', 'entity_id');
}

and

/**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function role()
    {
        return $this->belongsTo('App\AssignedRole', 'entity_id');
    }

but when i´m traying to call roles in my controller to show all roles from users, return this:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'gruposuite.entity_id' doesn't exist (SQL: select `assigned_roles`.*, `entity_id`.`user_id` as `pivot_user_id`, `entity_id`.`assigned_role_id` as `pivot_assigned_role_id` from `assigned_roles` inner join `entity_id` on `assigned_roles`.`id` = `entity_id`.`assigned_role_id` where `entity_id`.`user_id` in (1, 2, 3, 4, 5, 6, 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, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 76))

i don´t know that i´m doing wrong...

i hope that anybody can help me. thanks

Update

i can to do my relationship with:

model User

/**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function role()
    {
        return $this->hasMany('App\AssignedRole', 'entity_id');
    }

model AssignedRole

/**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
     public function user()
     {
        return $this->belongsTo('App\User');
     }

this it´s my result:

{"id":98,"role_id":3,"entity_id":1,"entity_type":"App\\User","restricted_to_id":null,"restricted_to_type":null,"scope":null},{"id":106,"role_id":9,"entity_id":1,"entity_type":"App\\User","restricted_to_id":null,"restricted_to_type":null,"scope":null},{"id":111,"role_id":7,"entity_id":1,"entity_type":"App\\User","restricted_to_id":null,"restricted_to_type":null,"scope":null},{"id":112,"role_id":5,"entity_id":1,"entity_type":"App\\User","restricted_to_id":null,"restricted_to_type":null,"scope":null},{"id":113,"role_id":6,"entity_id":1,"entity_type":"App\\User","restricted_to_id":null,"restricted_to_type":null,"scope":null},{"id":115,"role_id":4,"entity_id":1,"entity_type":"App\\User","restricted_to_id":null,"restricted_to_type":null,"scope":null}]}

But now i need role´s name how i can to do this?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire