简体   繁体   中英

How can I get role from database?

I want to get from database the role of users ( organization or individual).I have a table called Users, and another table called Role_users, where I have user_id with role_id ( 1 for individual and 100 for organization). I am not sure yet how to get because

@if($user->role=="Organizations")
                <i class="fa fa-building-o"></i>
                @else
                <i class="icon-user"></i>
                @endif

but I can't get the role for every user...This code is working on other page because of User.php where I found this code:

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();


<?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');
    }
}

see modified controller changin the count() to 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM