protected function formatResource(Resource $resource)
 {
     $descriptor = $resource->getDescriptor();
     // type and id fields are required.
     $data = ['type' => $descriptor->getName(), 'id' => $resource->getId()];
     // If resource has any attributes, add them.
     $attributes = $resource->getData();
     unset($attributes[$descriptor->getPrimaryKey()]);
     if (!empty($attributes)) {
         $data['attributes'] = $attributes;
     }
     // Include loaded relationships.
     if (!empty($loadedRelationships = $resource->getLoadedRelationships())) {
         $data['relationships'] = [];
         // For each different relationship...
         foreach ($loadedRelationships as $relationshipName => $relatedResources) {
             // We will include a whole mew JSON-API document, as per the specification.
             $document = new Document();
             $document->singleResource = !is_array($relatedResources);
             $document->resources = $relatedResources;
             $data['relationships'][$relationshipName] = $this->formatDocument($document);
         }
     }
     return $data;
 }
 public function save(Resource $resource)
 {
     $table = $resource->getDescriptor()->getName();
     $primaryKey = $resource->getDescriptor()->getPrimaryKey();
     $attributes = $resource->getData(true);
     if (is_null($resource->getId())) {
         return (bool) $this->create($resource->getDescriptor(), $attributes);
     }
     try {
         return (bool) $this->database->table($table)->where($primaryKey, $resource->getId())->update($attributes);
     } catch (QueryException $e) {
         return false;
     }
 }