laravel - 用户插件。创建模型后设置关系
问题描述
我通过 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';
}
解决方案
解决方案:
// User model
public function afterCreate() {
$this->groups()->add(UserGroup::where('code', 'registered')->first());
...
}
推荐阅读
- linux - jupyter lab - 抑制控制台输出
- php - Microsoft Graph api:订阅通知请求失败问题
- javascript - 尝试使用 javascript 删除 DOM 表
- hadoop - 为什么hadoop对reducer的输入进行排序?
- 2sxc - 在当前版本 9.33 中,在 Head 中包含 CSS 似乎不起作用
- javascript - 如何在使用 Ajax 成功单击提交按钮后隐藏模式?
- android - 如何更改片段中分离/附加的参数?
- java - Jacoco 报告 0 类
- html - 密钥不能重复 - 重新启动后无法运行此 index.html,即使它之前工作过
- ipfs - ipfs 节点可以查询远程对等点以列出其引脚吗?