mercredi 31 janvier 2018

Getting value from many to many relationship in Laravel

I'm trying to get value from my tables with many to many relationship. My relation are: User:

public function roles()
    {
        return $this->belongsToMany('App\Role',"users_roles","usersid","rolesid");
}

Role:

public function users()
    {
        return $this->belongsToMany('App\User',"users_roles","usersid","rolesid");
}

I did this in my view:

@foreach($users as $user)
                    <?php $i++; ?>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        <tr>

@endforeach

I'm getting following error: Undefined index: role

But when i do , I get following:

[{"id":1,"role":"Administrator","created_at":null,"updated_at":null,"pivot":{"usersid":4,"rolesid":1}}]

Can anyone tell me where did i go wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire