我想从数据库中获得用户的角色(组织或个人)。我有一个名为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();
$data['organizations'] = 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','=','organizations')
->count();
if ($role[0]['slug'] == 'individuals')
{
$data['role'] = $role[0]['slug'];
$id = $user->id;
$data['user'] = User::with('career_path.industry','career_path.department','career_path.functions','education.field_of_study','education.degree','privancy_setting')->where('username', '=' ,$username)->firstOrFail();
这是我的Role.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
// protected $fillable = [
// 'name', 'display_name', 'description',
// ];
protected $table = "roles";
public function user()
{
return $this->belongsToMany('App\User','role_users','role_id','user_id');
}
}
最佳答案
查看修改后的控制器将count()更改为get()
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')
->get();
$data['organizations'] = 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','=','organizations')
->get();
if ($role[0]['slug'] == 'individuals')
{
$data['role'] = $role[0]['slug'];
$id = $user->id;
$data['user'] = User::with('career_path.industry','career_path.department','career_path.functions','education.field_of_study','education.degree','privancy_setting')->where('username', '=' ,$username)->firstOrFail();