【发布时间】:2017-11-19 02:53:20
【问题描述】:
我有一个表(比如 myreferencetable),其名称列表为..say:
id | name | abbrv
- - - - - - - - - - - - - - - - -
1 | ABCDEF | abc
2 | TestState |
3 | UVWXYZ | xyz
在我的模型中,我为模型创建了一个属性:
protected $appends = [
'full_name'
];
现在, 我想查询数据库,以便获得以下详细信息:
[
{
"name" : ABCDEF
"full_name" : ABCDEF (abc)
},
{
"name" : TestState,
"full_name" : TestState
},
{
"name" : UVWXYZ,
"full_name" : UVWXYZ (xyz)
}
]
即字符串连接:
//强制 ( ) //如果不为空
现在,我的查询是:
public function getFullNameAttribute()
{
return MyReferenceTable::select(DB::raw('concat(name," (", abbrv, ")")'));
}
但它会返回:
[
{
"name" : ABCDEF
"full_name" : {}
},
{
"name" : TestState,
"full_name" : {}
},
{
"name" : UVWXYZ(xyz)
"full_name" : {}
}
]
我需要返回 name + ["(abbrv)"] // where [value] = if not null
【问题讨论】:
标签: php postgresql laravel eloquent concatenation