Devise\Pages\Collections\CollectionFieldsFactory::createFromCollectionInstance PHP Method

createFromCollectionInstance() public method

Creates collection fields object from a collection instance that we pulled from the database (those will have fields associated with them too)
public createFromCollectionInstance ( CollectionInstance $instance ) : CollectionFields
$instance CollectionInstance
return CollectionFields
    public function createFromCollectionInstance($instance)
    {
        $fields = $this->Field->where('collection_instance_id', '=', $instance->id)->where('page_version_id', '=', $instance->page_version_id)->get();
        return new CollectionFields($fields);
    }

Usage Example

Example #1
0
 /**
  * Get the list of collections for this page
  *
  * @param  DvsPageVersion $page
  * @return array($collectionName => array(CollectionFields))
  */
 public function findCollectionsForPageVersion(\DvsPageVersion $pageVersion)
 {
     $collections = array();
     // get a list of instances for this page
     // we order it by sort so that they are in order in our $collections array already
     $collectionInstances = $pageVersion->collectionInstances()->with('collectionSet')->orderBy('sort', 'ASC')->get();
     // loop over the instances and add it to the collections array
     foreach ($collectionInstances as $index => $collectionInstance) {
         // we want to add a dynamically created key name to the instance?
         // not being used in this context as far as I know of... but I'm
         // leaving this here just in case we need to bring it back at some point
         // $keyName = ($index + 1) . ') ' .$collectionInstance->name;
         $keyName = $collectionInstance->collectionSet->name;
         // make this an array if it is not already set
         $collections[] = isset($collections[$keyName]) ? $collections[$keyName] : [];
         // add these collection fields to this array
         $collections[$keyName][] = $this->CollectionFieldsFactory->createFromCollectionInstance($collectionInstance);
     }
     // put these instances into a Laravel collection
     foreach ($collections as $keyName => $collectionInstances) {
         $collections[$keyName] = new Collection($collectionInstances);
     }
     return $collections;
 }