简体   繁体   English

如何在 Eloquent ORM 中使用此方法?

[英]How can I make this method in Eloquent ORM?

Hlw, I am a new comer in php , laravel can you explain that How Can I Creat this is isAuthorized() method with laravel Elouquet by replacing this query builder Hlw,我是 php、laravel 的新来者,你能解释一下我如何用 laravel 替换这个查询生成器 Elouquet 来创建这个isAuthorized()方法吗?

class User extends Authenticatable
{
    public function isAuthorized($object, $operation)
    {
        return Db::table('role_permissions')
            ->where('object', $object)
            ->where('operation', $operation)
            ->join('user_roles', 'user_roles.role_id', '=', 'role_permissions.role_id')
            ->where('user_roles.user_id', $this->id)
            ->exists();
    }
}

Create your Models like so.像这样创建你的模型。

public class RolePermission {
    public function UserRoles() {
        return $this->hasMany(UserRole::class, 'role_id', 'role_id');
    }
}

public class UserRole {

}

WhereHas can query relationships and is the cornerstone to replacing joins. WhereHas可以查询关系,是替换联接的基石。

RolePermission::where('object', $object)
    ->where('operation', $operation)
    ->whereHas('UserRoles', function ($query) {
        $query->where('user_roles.user_id', $this->id)
    })     
    ->exists();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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