Laravel的Eloquent表与父母“继承”

我有一个Laravel模型 acl_groups </ code>,它有一个JSON列继承</ 代码>。 我应该怎么做,在检查一个组是否可以做某事时查询继承组的“laravel方式”? 权限存储在另一个JSON列中,允许/拒绝,因此我可以执行 in_array </ code>来检查单个组是否有权访问。</ p>
</ div>

展开原文

原文

I have a Laravel model acl_groups that has a JSON column inherits. What should I do, the "laravel way" to query the inherited groups when checking if a group can do something? The rights are stored in another JSON column, allow/deny so I can just do a in_array to check a single group if they have access.

dongliang2005
dongliang2005 查询继承的组是什么意思?你能提供示例代码,展示你是如何做到这一点以及你想怎么做的?
4 年多之前 回复

1个回答

On your model you can set a getter

public function getInheritsAttribute($v)
    {
        return $v ? json_decode($v, true) : [];
    }

OR if you dont want a getter you can try a pseudo getter

public function getPseudoAttribute()
        {
            return $this->inherits ? json_decode($this->inherits, true) : [];
        }

Kind of maybe did mistake on second one.

And on other model the same thing so when you call $item->inherits = you will get an array First you may try to prepare the array like removing same keys or values

and after just check

if (array_key_exists('thing_to_check', $item->inherits)) {
    return true;
}

This is not a working code, it is just an idea how you can do you. Take a look at Cartalyst Sentinel how they check the permissions for groups and users.

duandeng2011
duandeng2011 那么,继承数组理论上会包含其他组ID - 我如何有效地将它们转换为我的组模型的实例?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问