gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:27:40 30 4
gpt4 key购买 nike

我想从数据库中获取用户的 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 - 如何从数据库获取 Angular 色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664768/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com