Php Laravel eloquent:具有条件连接的模型属性
我有一个表(比如myreferencetable),其名称列表为..比如:Php Laravel eloquent:具有条件连接的模型属性,php,postgresql,laravel,eloquent,concatenation,Php,Postgresql,Laravel,Eloquent,Concatenation,我有一个表(比如myreferencetable),其名称列表为..比如: id | name | abbrv - - - - - - - - - - - - - - - - - 1 | ABCDEF | abc 2 | TestState | 3 | UVWXYZ | xyz 在我的模型中,我为模型创建了一个属性: protected $appends = [ 'full_name' ]; 现在,, 我想查询D
id | name | abbrv
- - - - - - - - - - - - - - - - -
1 | ABCDEF | abc
2 | TestState |
3 | UVWXYZ | xyz
在我的模型中,我为模型创建了一个属性:
protected $appends = [
'full_name'
];
现在,,
我想查询DB,以便获得以下详细信息:
[
{
"name" : ABCDEF
"full_name" : ABCDEF (abc)
},
{
"name" : TestState,
"full_name" : TestState
},
{
"name" : UVWXYZ,
"full_name" : UVWXYZ (xyz)
}
]
i、 e.串接:
//必须 - (
)//如果不为null
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)]//其中[value]=如果不是null
试试这个方法
public function getFullNameAttribute()
{
return $this->name . ($this->abbrv ? ' (' . $this->abbrv . ')' : '');
}
试试这个方法
public function getFullNameAttribute()
{
return $this->name . ($this->abbrv ? ' (' . $this->abbrv . ')' : '');
}
试试这个
public function getFullNameAttribute()
{
if( ! $this->abbrv)
return $this->name;
return sprintf('%s (%s)', $this->name, $this->abbrv);
}
试试这个
public function getFullNameAttribute()
{
if( ! $this->abbrv)
return $this->name;
return sprintf('%s (%s)', $this->name, $this->abbrv);
}
曾为:
public function getFullNameAttribute()
{
return trim($this->name . (is_null($this->abbrv)? "" : " (" . $this->abbrv . ")"));
}
曾为:
public function getFullNameAttribute()
{
return trim($this->name . (is_null($this->abbrv)? "" : " (" . $this->abbrv . ")"));
}
您可以(ab)使用NULL
-处理CONCAT()
函数和连接运算符|
之间的差异,即:
CONCAT(name, ' (' || abbrv || ')')
应该这样做。您可以(ab)使用NULL
-处理CONCAT()
函数和连接运算符|
之间的差异,即:
CONCAT(name, ' (' || abbrv || ')')
我们应该做到这一点