Download the PHP package larsmbergvall/json-api-resources-for-laravel without Composer

On this page you can find all versions of the php package larsmbergvall/json-api-resources-for-laravel. 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 json-api-resources-for-laravel

JSON:API Resources for Laravel

Table of Contents

Example

This is an example of the output generated by this package:

Returns this:

Installation

You can install the package via composer:

It is recommended to use the middleware, either on all API calls:

Or, on a per-route basis:

Usage

Attributes

This package uses PHP 8 attributes to annotate Eloquent models. There is no need for additional resource classes. Attributes are used like this:

#[JsonApiType(string)]

This attribute can be used to change the value of the type property in the final output json. This attribute is optional. If it is not used, type will be set to the singular version of the models name, i.e. 'type' => 'book'.

#[JsonApiIncludeAttributes(string[])]

This attribute can be used to specify which of the models attributes should be included in the final output json. This attribute is optional. If it is not used, all visible model attributes will be included. It uses $model->attributesToArray() to get the attributes, so it will respect any $hidden fields.

#[JsonApiIncludeRelationships(string[])]

This attribute can be used to specify which of the models relationships should be included. This attribute is optional. If it is not used, no relationships will be included in the final output json.

NOTE: Regardless of what is included by this attribute, JsonApiResource only includes eager loaded relationships.

Creating JsonApiResources

If you have a single model, you should use JsonApiResource:

If you have a collection of models, you should use JsonApiResourceCollection:

If you need to manually return a JSON:API response, or don't want to use the middleware, you can use the jsonApiResponse()-helper:

It sets the correct Content-Type header for JSON:API responses. Note that it doesn't transform validation errors.

Middleware

The middleware primarily takes care of two things:

  1. Making sure the response that is returned has the correct headers etc.
  2. Transforming Laravels validation errors into the expected JSON:API error format

The middleware should be executed after any other middlewares that could change the Content-Type header of the response.

The JsonApiResource class

This class has a few public methods that you can use. Most likely you will not need to, because the JsonApiResourceCollection class handles that stuff automatically.

However, there is an important method: withIncluded(). By default, a JsonApiResource does not include it's related data. If you however run $resource->withIncluded() (or JsonApiResource::make($book)->withIncluded()) it will do so.

NOTE: It only includes eager loaded relationships, so if you need to exclude something for a certain api route you can just choose not to load a relationship. However, it includes related data recursively, meaning that it also includes data related to the included data.

The JsonApiResourceCollection class

This class also has the withIncluded() method that you can use to include any related data to your resources. Just as with the JsonApiResource, it only includes relations that are already loaded. However, it includes related data recursively, meaning that it also includes data related to the included data.

Error transformation

When validation fails in Laravel, normally you get a response that looks like this:

But with the JsonApiMiddleware, this response changes to:

Testing

There are some testing utilities on the JsonApiResource and JsonApiResourceCollection classes that you can utilize to test your application.

Note that more testing utilities will be coming in future versions!

assertHasData

assertDoesntHaveData

This is the opposite of assertHasData. It will fail if it finds the expected data.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of json-api-resources-for-laravel with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1|^8.2
spatie/laravel-package-tools Version ^1.14.0
illuminate/contracts Version ^9.34|^10.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 larsmbergvall/json-api-resources-for-laravel contains the following files

Loading the files please wait ....