PHPackages                             navjobs/transmit - 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. navjobs/transmit

AbandonedLibrary[API Development](/categories/api)

navjobs/transmit
================

A collection of Laravel api base classes and helpers.

0.8.1(8y ago)73.9k2[1 issues](https://github.com/NavJobs/Transmit/issues)[1 PRs](https://github.com/NavJobs/Transmit/pulls)MITPHPPHP &gt;=5.5.0

Since Dec 29Pushed 8y ago6 watchersCompare

[ Source](https://github.com/NavJobs/Transmit)[ Packagist](https://packagist.org/packages/navjobs/transmit)[ Docs](https://kimmel.com)[ RSS](/packages/navjobs-transmit/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (7)Versions (33)Used By (0)

[![Circle CI](https://camo.githubusercontent.com/17587f40d88c211f1f23bd6f1c50c8edaa71a6b25c24ca53ba323f122976e94f/68747470733a2f2f636972636c6563692e636f6d2f67682f6e61766a6f62732f7472616e736d69742e7376673f7374796c653d736869656c64)](https://circleci.com/gh/navjobs/transmit)[![Coverage Status](https://camo.githubusercontent.com/b175452e20d1706c7340353a080f9bd3188ac8758ba8c726f46f58fed068d0ca/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f4e61764a6f62732f5472616e736d69742f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/NavJobs/Transmit?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b7e4a70d06c7bcaa8cf191ed29a0a4edfea8c713b97ef6b4b7eddb24fdb75302/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4e61764a6f62732f5472616e736d69742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/NavJobs/Transmit/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/f0af33027d6af9c5be9dde78f50c03c6a841c103c69d6222533de152a6e53e92/68747470733a2f2f706f7365722e707567782e6f72672f6e61766a6f62732f7472616e736d69742f762f737461626c65)](https://packagist.org/packages/navjobs/transmit) [![Total Downloads](https://camo.githubusercontent.com/b99317f39ce0c2f547e03bd99039f879d0be459cd2e30ab363e30969b927ec47/68747470733a2f2f706f7365722e707567782e6f72672f6e61766a6f62732f7472616e736d69742f646f776e6c6f616473)](https://packagist.org/packages/navjobs/transmit) [![License](https://camo.githubusercontent.com/b35c12f90e965402da6c75a5243f29715978b0a74948b888cf7c7efd964956c6/68747470733a2f2f706f7365722e707567782e6f72672f6e61766a6f62732f7472616e736d69742f6c6963656e7365)](https://packagist.org/packages/navjobs/transmit)

###### Communication Layer For Laravel

[](#communication-layer-for-laravel)

Transmit was created to expedite the process of implementing REST APIs.

#### Install

[](#install)

Via Composer:

```
$ composer require NavJobs/Transmit
```

Register the service provider in your config/app.php:

```
'providers' => [
    ...
    NavJobs\Transmit\TransmitServiceProvider::class,
];
```

The package has a publishable config file that allows you to chang the default serializer. To publish:

```
php artisan vendor:publish --provider="NavJobs\Transmit\TransmitServiceProvider"
```

#### Api

[](#api)

Transmit provides an abstract controller class that you should extend from:

```
use NavJobs\Transmit\Controller as ApiController;

class BookController extends ApiController
{
...
```

The controller class provides a number of methods that make API responses easy:

```
//Return the specified item, transformed
$this->respondWithItem($item, $optionalTransformer);

//Sets the status code to 201 and return the specified item, transformed
$this->respondWithItemCreated($item, $optionalTransformer);

//Return the specified collection, transformed
$this->respondWithCollection($collection, $optionalTransformer);

//Paginate the specified collection
$this->respondWithPaginatedCollection($collection, $optionalTransformer, $perPage = 10);

//Set the status code to 204, and return no content
$this->respondWithNoContent();
```

Also provided are a number of error methods. These return an error response as well as setting the status code:

```
//Sets the status code to 403
$this->errorForbidden($optionalMessage);

//Sets the status code to 500
$this->errorInternalError($optionalMessage);

//Sets the status code to 404
$this->errorNotFound($optionalMessage);

//Sets the status code to 401
$this->errorUnauthorized($optionalMessage);

//Sets the status code to 400
$this->errorWrongArgs($optionalMessage);
```

The controller also uses a the QueryHelperTrait that aids with applying query string parameters to Eloquent models and query builder instances:

```
//Eager loads the specified includes on the model
$this->eagerLoadIncludes($eloquentModel, $includes);

//Applies sort, limit, and offset to the provided query Builder
$this->applyParameters($queryBuilder, $parameters);
```

#### Transformers

[](#transformers)

Transmit provides an abstract transformer class that your transformers should extend from:

```
use NavJobs\LaravelApi\Transformer as BaseTransformer;

class BookTransformer extends BaseTransformer
...
```

The transformer allows you to easily determine which relationships should be allowed to be eager loaded. This is determined by matching the requested includes against the available and default includes.

```
//Pass in either an array or csv string
//Returns an array of includes that should be eager loaded
$this->getEagerLoads($requestedIncludes);
```

#### Implementation Example

[](#implementation-example)

These methods can be combined to quickly create expressive api controllers. The following is an example of what that implementation might look like:

```
