mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 07:02:47 +08:00
Model类append方法支持追加关联对象属性
This commit is contained in:
@@ -24,6 +24,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
protected $visible = [];
|
||||
// 隐藏属性
|
||||
protected $hidden = [];
|
||||
// 追加属性
|
||||
protected $append = [];
|
||||
|
||||
public function __construct($items = [])
|
||||
{
|
||||
@@ -69,6 +71,19 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置需要追加的输出属性
|
||||
* @access public
|
||||
* @param array $append 属性列表
|
||||
* @param bool $override 是否覆盖
|
||||
* @return $this
|
||||
*/
|
||||
public function append($append = [], $override = false)
|
||||
{
|
||||
$this->append = [$append, $override];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
$result = [];
|
||||
@@ -79,6 +94,9 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
} elseif (!empty($this->hidden)) {
|
||||
$item->hidden($this->hidden[0], $this->hidden[1]);
|
||||
}
|
||||
if (!empty($this->append)) {
|
||||
$item->append($this->append[0], $this->append[1]);
|
||||
}
|
||||
$result[$key] = $item->toArray();
|
||||
} else {
|
||||
$result[$key] = $item;
|
||||
|
||||
Reference in New Issue
Block a user