Download the PHP package themsaid/laravel-model-transformers without Composer

On this page you can find all versions of the php package themsaid/laravel-model-transformers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-model-transformers

Laravel 5 Model Transformers

Latest Version on Packagist Total Downloads

This package helps API developers to easily transform Eloquent models into arrays that are convertible to JSON.

Here's how you use it, let's say you have a model with the following data:

Here you use a numerical value to represent the different types, you also have a mutator in the model that maps the numerical value to a string.

Inside the controller we can transform the model to be represented into more API friendly structure.

The above code will result a JSON string that may look like this:


Installation

Begin by installing the package through Composer. Run the following command in your terminal:

Once composer is done, add the package service provider in the providers array in config/app.php

Finally publish the config file:

That's all what we need.

Usage

Create a model transformer class by extending the AbstractTransformer class:

Now you can call the transformer from any controller:

You can also pass a collection and the result will be an array of transformed models:

Dealing with relationships

The package contains two helpful methods for dealing with relationships, the first one helps you know if a specific relation is eager-loaded:

isRelationshipLoaded()

Now only if the tags are eager-loaded they will be presented in the $output array, this helps reminding you to eager-load when querying models with relationships.

isLoadedFromPivotTable()

This method helps you know if the model is loaded from a ManyToMany relationship, it's helpful when there are pivot data in the table and you would like to present them, example for that:


Passing options to the transformer

You may need to pass some options from the controller to the transformer, you can do that by providing an array of options to the transform() method as a second parameter:

Now from inside the CategoryTransformer you can check the options parameter:

Using the shorthand method

This package is shipped with a shorthand method for applying transformation for a Model or a Collection:

Using the transform() method, the package locates the suitable transformer based on the Model or the Collection passed as the first argument.

By default it assumes that all transformers are located under the App\Transformers namespace, you can change this behaviour in the config file.

You may also pass options to the transformer as a second argument:


All versions of laravel-model-transformers with dependencies

PHP Build Version
Package Version
Requires illuminate/support Version ~5.1
php Version >=5.3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package themsaid/laravel-model-transformers contains the following files

Loading the files please wait ....