[英]Laravel changing the attribute name on eloquent
I have a group
attribute in my model and I want to change the group attribute to sub-group
if the group parent is not 0. 我的模型中有一个
group
属性,如果组父级不为0,我想将组属性更改为sub-group
。
My code: 我的代码:
public function setGroupAttribute
{
$group_id = $this->attribute['id'];
$group = Group::find($group_id);
if ($group->parent != 0)
{
$this->attribute['group'] = 'sub-group';
}
}
But I guess what I'm doing is wrong? 但是我想我在做什么错了? So how do I do this right?
那么我该怎么做呢? I just want to change the attribute and not the data inside.
我只想更改属性而不是内部的数据。
You are setting $this->attribute['group']
to a string of 'sub-group' rather than re-indexing it. 您将
$this->attribute['group']
设置为字符串“ sub-group”,而不是对其重新编制索引。 Try this instead... 试试这个...
$this->attribute['sub-group'] = $this->attribute['group'];
unset($this->attribute['group']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.