0

What does the error mean and how do I fix it?

( Notice: Trying to access array offset on value of type null in....)

public function hasPermission($key)
{
    $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
    if ($group->count()) {
        $permissions = json_decode($group->firstResult()->permissions, true);

        if ($permissions[$key] == true) {
            return true;
        }
    }

    return false;
}

The error is on this line:

if ($permissions[$key] == true) {

Regards

nSo
  • 1
  • 1

2 Answers2

1

You don't need to do

if ($permissions[$key] == true) {
     return true;
}

Rather try this:

if (isset($permissions[$key]) && $permissions[$key]) {
    return true;
}

Hope this works!!

Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
-1

It means that you're trying to get data from you're array while the parameter returns NULL in $permissions[$key]. So check if you're $key has any value.