PHPackages                             aaronbullard/dogpile - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. aaronbullard/dogpile

ActiveLibrary[API Development](/categories/api)

aaronbullard/dogpile
====================

JSON:API helper library to cleanly import included relationships

v1.0(6y ago)48MITPHPPHP &gt;=7.0CI failing

Since Jun 28Pushed 6y ago3 watchersCompare

[ Source](https://github.com/aaronbullard/dogpile)[ Packagist](https://packagist.org/packages/aaronbullard/dogpile)[ RSS](/packages/aaronbullard-dogpile/feed)WikiDiscussions develop Synced today

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

Dogpile
=======

[](#dogpile)

[![Maintainability](https://camo.githubusercontent.com/62abc60824335b434b85a263a2641a0432a964051cbad0853b0d0765db042aef/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f66643239616365366564363866353236393036612f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/aaronbullard/dogpile/maintainability)

[![](./img/dogpile.jpg)](./img/dogpile.jpg)

JSON:API helper library to cleanly import included relationships

Installation
------------

[](#installation)

### Library

[](#library)

```
git clone git@github.com:aaronbullard/dogpile.git
```

### Composer

[](#composer)

[Install PHP Composer](https://getcomposer.org/doc/00-intro.md)

```
composer require aaronbullard/dogpile
```

### Testing

[](#testing)

```
composer test
```

Usage
-----

[](#usage)

First, visit  for documentation on JSON:API standard. This library attempts to simplify and modularize included related resource objects in api calls.

```
Ex. GET /posts/1?include=author,comments,comments.author

```

This library will quickly query and include the related 'author' and 'comments' assosciated with the resource. To do so, requires a few interface implementations.

```
// Example contoller method for GET /posts/1?include=author,comments,comments.author
public function show(Request $httpRequest): JsonResponse
{
    $postId = $httpRequest->get('postId');
    $includes = explode(',', $httpRequest->get('include')); //['author', 'comments', 'comments.author'];

    // In this example, $post implements the Dogpile\Contracts\Resource interface;
    $post = $this->myPostRepo->find($postId);

    // Example of using Dogpile\ResourceManager::class
    $includedResources = $this->resourceManager->newQuery()
        ->setRelationships($post->relationships())
        ->include(...$includes)
        ->query();

    return new JsonResponse([
        'data' => $post->toJsonapi(), // or however you want to transform your model
        'included' => array_map(function($resource){
            return $resource->toJsonapi();
        }, $includedResources);
    ], 200);
}
```

How it works
------------

[](#how-it-works)

The Dogpile\\ResourceManager class contains several ResourceQuery objects for each resource type. When an array of included relationships are provided, the Dogpile\\QueryBuilder class will use the ResourceIdentifiers from the RelationshipCollection and query the resources via the RepositoryQuery::findHavingIds() method. Each resource will only be queried once.

Setup
-----

[](#setup)

### First

[](#first)

Dogpile requires a Dogpile\\Contracts\\ResourceQuery interface implementation for each resource. As in the above example, you would want an implementation for 'people' (to query authors); and one for 'comments'.

```
