本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection::merge方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::merge方法的具体用法?PHP Collection::merge怎么用?PHP Collection::merge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Collection
的用法示例。
在下文中一共展示了Collection::merge方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getRoles
/**
* Get all roles as collection.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getRoles()
{
if (!$this->roles) {
$this->roles = $this->grantedRoles()->get();
$deniedRoles = $this->deniedRoles()->get();
foreach ($deniedRoles as $role) {
$deniedRoles = $deniedRoles->merge($role->descendants());
}
foreach ($this->roles as $role) {
if (!$deniedRoles->contains($role)) {
$this->roles = $this->roles->merge($role->descendants());
}
}
$this->roles = $this->roles->filter(function ($role) use($deniedRoles) {
return !$deniedRoles->contains($role);
});
}
return $this->roles;
}
示例2: save
/**
*
*/
public function save()
{
if (empty($this->data)) {
return;
}
if ($this->relation instanceof Relations\HasMany && is_array($this->data)) {
if (is_numeric($this->data[0])) {
$data = new Collection();
foreach ($this->data as $key => $item) {
// $this->data[$key] = $this->related->baseModel->find($item);
$data->push($this->related->baseModel->find($item));
}
} else {
$data = new Collection($this->data);
}
// detach any existing models and only save the selected ones
$foreignKey = $this->relation->getPlainForeignKey();
$current = $this->relation->getResults();
if (!$current) {
$this->relation->saveMany($data->toArray());
return;
}
$all = $data->merge($current);
foreach ($all as $item) {
if ($keep = $data->find($item->getKey())) {
$this->relation->save($keep);
} else {
$item->{$foreignKey} = null;
$item->save();
}
}
} else {
if ($this->relation instanceof Relations\BelongsToMany && is_array($this->data)) {
if (is_numeric($this->data[0])) {
$this->relation->sync($this->data);
}
} else {
/* If we have an id let's grab the model instance, otherwise assume we were given it */
$this->data = is_numeric($this->data) ? $this->related->baseModel->find($this->data) : $this->data;
parent::saveRelation($this->relation, $this->data);
}
}
}