我的想法是,我需要显示用户帐户的状态,无论是Active
还是Inactive
在 Blade 文件中,因此在数据库中有事件列,他的默认值为null
,在 Blade 中时,如果要显示用户的状态,我需要执行此操作:
<td>{{ $user->active ? "active" : 'Inactive' }}</td>
所以我搜索了一下,发现了这种缩短上面代码的方法:
public function getActiveBladeAttribute($a)
{
return [
null =>'Inactive',
1 =>'Active',
][$a];
}
刷新我的页面时它不起作用:( 并显示此错误
Undefined index
高亮显示错误 ( null =>'Inactive' )。
那么针对这些情况走捷径的最佳方法是什么?
最佳答案
如果 getActiveBladeAttribute()
在您的 User
模型上,您只需使用 $this
即可访问当前用户的变量:
public function getActiveBladeAttribute(){
return $this->active ? 'Active' : 'Inactive';
}
并且,在您看来,只需调用:
{{ $user->activeBlade }}
通常,在 get{Param}Attribute()
函数中,您不需要传递任何内容,因为 $this
可用。
关于laravel - 活跃账户值(value)如何安排?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59414432/