javascript - 如何从数据库获取角色?

标签 javascript php html laravel

我想从数据库中获取用户的 Angular 色(组织或个人)。我有一个名为“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

但我无法获取每个用户的 Angular 色...此代码可以在其他页面上运行,因为我在 User.php 中找到了此代码:

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

这是我的 Controller

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

最佳答案

查看修改后的 Controller 将 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();

关于javascript - 如何从数据库获取角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664768/

相关文章:

javascript - Ajax请求返回空数组

Javascript 智能感知不可用

php - 如何将 mysql php 错误写入文本文件

javascript - 是否可以使用 css 媒体查询获取移动设备的大小并将其发送到 php 变量

javascript - 如何从下面的代码中无限翻转?

javascript - 在 Angular 中安装离线 Bootstrap

javascript - Array.isArray() 不适用于 NodeList。还有其他选择吗?

javascript - Ajax php 登录表单不指向另一个页面

java - 无法连接到000webhost.com的Mysql数据库

html - 防止导航栏水平收缩