[英]Accessor not working in Laravel 5.4
I am new on Laravel 5.4 and currently working on small project on Laravel. 我是Laravel 5.4的新手,目前正在Laravel上从事小型项目。 As per my client requirements, I have renamed some of columns of tables with CamelCase standard which is against of Laravel naming convention.
根据我的客户要求,我已使用CamelCase标准对表的某些列进行了重命名,这违反了Laravel命名约定。
For example, My table "A" has columns "Status" and "FirstName" instead of "status" and "first_name". 例如,我的表“ A”具有列“状态”和“名字”,而不是“状态”和“名字”。
Now, I am using accessor in model file to modify value of these columns by creating function given below: 现在,我在模型文件中使用存取器通过创建以下给出的函数来修改这些列的值:
public function getStatusAttribute($val) {
reutrn $val . ' Status';
}
public function getFirstNameAttribute($val) {
return 'User is '.$val;
}
When I am fetching data using elequent process, then system is returning value of these as blank. 当我使用后续过程获取数据时,系统将这些值作为空白返回。
If I rename the column name according to "Laravel Naming Convention" then it is giving me right results. 如果我根据“ Laravel命名约定”重命名列名,那么它给我正确的结果。
I don't know what is the exact issue here. 我不知道确切的问题在这里。 Please anyone can share your solution to fix this problem?
请任何人都可以分享您的解决方案来解决此问题?
Access the fields directly. 直接访问字段。
public function getStatusAttribute()
{
return $this->Status . ' Status';
}
public function getFirstNameAttribute()
{
return 'User is ' . $this->FirstName;
}
Add this to the model to have these fields appended every time. 将此添加到模型以每次都附加这些字段。
protected $appends = [
'status', 'first_name',
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.