当前位置: 首页>>代码示例>>PHP>>正文


PHP Collection::merge方法代码示例

本文整理汇总了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;
 }
开发者ID:askedio,项目名称:laravelcp,代码行数:24,代码来源:HasRoleAndPermission.php

示例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);
         }
     }
 }
开发者ID:packettide,项目名称:bree,代码行数:46,代码来源:Relate.php


注:本文中的Illuminate\Database\Eloquent\Collection::merge方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。