如何遍历数组以获取项目?

时间:2020-06-06 08:18:24

标签: php laravel laravel-7

am试图遍历数组$permission_ids以获取值68并将其分配给$manager_role并将此值存储在表permissions中,但是此块代码不能做到这一点? 我在哪里出错了?

**

foreach ($permission_ids as $permission_id) {
      if($permission_id = 68){
        // attach manager role permission to manager role
        $manager_role->permissions()->attach($permission_id);
      }

    }

**

1 个答案:

答案 0 :(得分:1)

我认为@Hoppo的注释是正确的,您弄错了等号运算符,请使用==而不是=

   foreach ($permission_ids as $permission_id) {
      if($permission_id == 68){
        // attach manager role permission to manager role
        $manager_role->permissions()->attach($permission_id);
      }

    }