Download the PHP package netsells/laravel-resourceful without Composer

On this page you can find all versions of the php package netsells/laravel-resourceful. 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-resourceful

Laravel Resourceful

Package that helps you use Laravel API Resources without killing your database! Say goodbye to N+1 :wave:

The Problem

Picture a User model that has an "avatar" relation to a File model:

To return a list of users from our API, we could have such a controller action:

In the example above:

We have a classic N+1 problem. The usual solution in Laravel is to perform eager loading within the controller:

This works, but has few problems:

This package fixes all those problems and makes the N+1 problem within your API a thing of the past!

Installation

using composer:

Usage

This package is a drop in replacement for the official API Resources. Instead of extending the builtin Illuminate\Http\Resources\Json\JsonResource we extend Netsells\Http\Resources\Json\JsonResource.

Continuing with the example above, we get:

Of course, this doesn't change anything and things still work as they did with N+1 problem, however this first step allows you to easily switch over to this package.

There are few ways now to supercharge your API resource.

Preloads method

First way is to select the relations for eager loading in a dedicated preloads method. You should return one or more "deferred resources", which can be easily created by calling preload helper with a list of relations you want loaded. The preloads method can optionally accept a Laravel Request as its first argument. Within the preloads method we have access to the Eloquent model being resolved. This allows us to introduce conditional logic for preloading specific relations based on request parameters / model attributes.

Now before the toArray is called, the avatar relation will be loaded for all the users in collection, so we've got full access to $this->avatar without issuing further queries.

More examples can be seen in the tests.

Use callback method

Second way is to provide a callback which will be called with the resolved relations.

Now when toArray is called, the avatar relation hasn't been loaded yet. Our code in the toArray method will execute for all the users in collection, and each call to the use method will queue a relation to be loaded. We'll then resolve all the queued relations for all users in the most optimal way, and invoke the callback function with resolved relations.

More examples can be seen in the tests.

Inline method

Third way is to use an inline shortcut which is very convenient for loading one or many nested API Resources.

In the example above, we have introduced a nested resource, where a UserResource embeds a separate AvatarResource. Each resource is responsible for eager loading its own relations, and together all relations are loaded in most optimal way.

More examples can be seen in the tests.


All versions of laravel-resourceful with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1 || ^8.2
ext-json Version *
laravel/framework Version ^9.0 || ^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 netsells/laravel-resourceful contains the following files

Loading the files please wait ....