我想从数据库中获取用户(组织或个人)的角色。我有一个名为 Users 的表,另一个名为 Role_users 的表,其中我有 user_id 和 role_id(个人为 1,组织为 100)。我还不确定如何获取,因为我正在使用此代码:@if($user->role=="Organizations") <i class="fa fa-building-o"></i> @else <i class="icon-user"></i> @endif但我无法获得每个用户的角色......由于 User.php ,我在其他页面上找到了此代码,因此该代码正在其他页面上运行:public function role() { return $this->belongsToMany('App\Role','role_users','user_id','role_id'); }这是我的控制器public function viewProfile($username) { $data = $this->data; $user = User::with('role')->where('username', '=' ,$username)->firstOrFail(); $categoryID = \App\Category::pluck('id'); $role = []; foreach ($user->role as $key => $value) { $role[$key]['slug'] = $value->slug; } if(Sentinel::check()) { $user = User::findOrfail($user->id); $invitation = \App\Invitation::where('inviter_id', '=' ,Sentinel::check()->id)->where('target_id', '=' ,$user->id)->count(); $target = \App\Invitation::where('inviter_id', '=' ,$user->id)->where('target_id', '=' ,Sentinel::check()->id)->count(); $data['request'] = $invitation + $target; } $data['individuals'] = DB::table('contacts') ->join('users' , 'users.id', '=', 'contacts.contact_id') ->join('role_users','role_users.user_id','=','users.id') ->join('roles','roles.id','=','role_users.role_id') ->select('users.*') ->where('contacts.user_id','=',$user->id) ->where('roles.slug','=','individuals') ->count();
添加回答
举报
0/150
提交
取消