2017-05-01 25 views
2

나는 권한 부여 및 인증을 위해 Sentinel Framework를 사용합니다. 그러나 나는 사용자 관리에 머물렀다.Laravel/Sentinel - 피벗 테이블을 통해 가치를 얻는 방법

이메일 - 역할과 같은 세부 정보를 표시하고 싶지만 여기에 여전히 붙어 있습니다.

내 데이터베이스 :

role: ID, slug, name 
user: ID, Email, ... 
role_users: user_id, role_id 

내 컨트롤러

public function getAll(){ 
    $user = User::all(); 
    return view('admin.user.list',['user'=>$user]); 
} 

내 UserModel 그런

public function Role(){ 
    return $this->belongsToMany('App\Role','role_users','user_id', 'role_id'); 
} 

내보기

<?php foreach ($user as $u): ?> 
<tr> 
<td>{{$u->Role->name}}</td> 
</tr> 
<?php endforeach; ?> 

그때 나는

<td>{{$u->Role}}</td> 

그건이

[{"id":1,"slug":"admin","name":"admin","permissions":null,"created_at":null,"updated_at":null,"pivot":{"user_id":1,"role_id":1}}] 

같은 배열을 표시 작성하는 경우 오류

Property [slug] does not exist on this collection instance. 

있어하지만 난 "슬러그"속성에 액세스 할 수 없습니다 :(

어떻게하면됩니까?

감사합니다.

답변

0

사용자 모델

class User extends Model 
    { 
     /** 
     * The roles that belong to the user. 
     */ 
     public function roles() 
     { 
      return $this->belongsToMany('App\Role'); 
     } 
    } 

모범

class Role extends Model 
{ 
    /** 
    * The users that belong to the role. 
    */ 
    public function users() 
    { 
     return $this->belongsToMany('App\User'); 
    } 
} 

제어기

public function getAll(){ 
    $user = User::all(); 
    return view('admin.user.list',['user'=>$user]); 
} 

보기

@foreach ($user as $u) 
    // becuase many to many you need this 
    @foreach ($u->roles as $role) 
     {{$role->name}} 
    @endforeach 
@endforeach 

수표 Laravel Many To Many Document 자세한 내용은 Laravel Many To Many

+0

안녕하세요 Mortadda Jafar. 도움 주셔서 감사하지만 작동하지 않습니다. 나는 아직도 테이블이나 뷰를 찾을 수 없다. 1146 테이블 'shop.role_user'가 존재하지 않는다. (SQL :'roles'. *,'role_user'.user_id'를'pivot_user_id','role_user' 'role_id''는'roles'에서'pivot_role_id'로 내부 join'' role_user''''''''''''''''''role_user'.'role_id''''''는'role_user'.'user_id' = 1로 나타납니다. 여전히 Many many documents를 다시 읽었지만 대답을 찾지 못했습니다. (도움을 주셔서 감사합니다.) –

+0

"shop.role_user"란 무엇입니까? –

+0

role_user는 user_id와 role_id 두 개의 필드가 있습니다. 여기 내 DB 역할은 ID, 슬러그입니다. , 이름 | | 사용자 : ID, 전자 메일, ... | | role_users : user_id, role_id | –