當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。