dongqiaogouk86049 2016-02-01 00:24
浏览 66

Laravel的Eloquent表与父母“继承”

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.

  • 写回答

1条回答

  • dongyinpan9284 2016-02-01 15:53
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退