首页 > 解决方案 > 用户插件。创建模型后设置关系

问题描述

我通过 Rainlabs 自定义用户插件。

用户模型与用户组有关系。创建用户后,我想设置他的用户组(访客、注册等)。

它不工作:

// also tried beforeCreate
public function afterCreate()
{
    if (something)
       // groups is a relation field name
       $this->groups = 'guest'; // guest is a user group code
    else
       $this->groups = 'registered';
}

标签: laraveloctobercms

解决方案


解决方案:

// User model
public function afterCreate() {
    $this->groups()->add(UserGroup::where('code', 'registered')->first());
    ...
}

推荐阅读