Laravel Spatie获得用户角色

时间:2020-09-18 11:47:04

标签: php laravel permissions spatie

模型文件

class BanUser extends Model
    {        
    public function user()
                {
                    return $this->belongsTo(User::class, "user_id", "id");
                }   
        
         public static function getBanList()
                {
                  
                    $getlist = BanUser::with(['user'])
                                ->get()
                                ->toArray();
            
                    return $getlist;
                    
                }
    }

//数据

[0] => Array
                (
                    [id] => 3
                    [user_id] => 2
                    [created_at] => 2020-09-16T16:06:54.000000Z
                    [updated_at] => 2020-09-16T16:06:54.000000Z
                    [user] => Array
                        (
                            [id] => 2
                            [name] => hi
                            [email] => hi@gmail.com
                            [email_verified_at] => 
                            [created_at] => 2020-09-10T12:18:18.000000Z
                            [updated_at] => 2020-09-10T12:19:11.000000Z
                   
                        )

                )

上面的代码是检索所有禁令用户列表,但是每个用户都附加了一个角色,我如何加入角色表以便每个用户检索角色?我使用了laravel spatie权限。有人可以帮忙吗? :(

2 个答案:

答案 0 :(得分:2)

我想你可以用这个

func requestAuth(serviceParams: AuthServiceParams) -> Future<AuthResponse, Error> {
    Future { promise in
        AlamofireService.auth(serviceParams: serviceParams).responseObject { response in
            guard response.error == nil else {
                print("❌⭕️❌ Auth login hatalı bir dönüş aldı sorun var.")
                promise(.failure(response.error!))
                return
            }
            guard let data = response.result.value else {
                let error = NSError(domain: "Bir sorun oluştu. Lütfen yöneticinize başvurunuz.", code: 1001, userInfo: nil)
                promise(.failure(error))
                return
            }
            guard let token = data.data?.token else {
                print("TOKEN BULUNAMADI")
                let authResponse = AuthResponse(
                    result: "fault",
                    success: false,
                    data: nil,
                    message: "Kullanıcı adı veya şifre hatalı",
                    errCode: "E0000"
                )
                promise(.success(authResponse))
                return
            }
            print("AuthLogin Token ------->   \(token)")
            ApplicationVariables.token = token
            ApplicationVariables.customer = data.data?.customer
            ApplicationVariables.config = data.data?.store?.config
            ApplicationVariables.logo = data.data?.store?.logo
            promise(.success(data))
        }
    }
}

public function user() { return $this->belongsTo(User::class, "user_id", "id")->with('roles'); } roles添加,因此您可以使用此

这是角色模型https://github.com/spatie/laravel-permission/blob/master/src/Models/Role.php

您可以使用的所有可用功能 https://github.com/spatie/laravel-permission/blob/master/src/Traits/HasRoles.php


以另一种方式

laravel spatie

答案 1 :(得分:0)

你可以像这样拨出idname

$request->user()->roles()->pluck('name','id');