LaravelPackages.net
Acme Inc.
Toggle sidebar
avertys/lighter

Make your requests lighter

8
0
1.0.3
About avertys/lighter

avertys/lighter is a Laravel package for make your requests lighter. It currently has 0 GitHub stars and 8 downloads on Packagist (latest version 1.0.3). Install it with composer require avertys/lighter. Discover more Laravel packages by avertys or browse all Laravel packages to compare alternatives.

Last updated

LIGHTER

Lighter is a package htaht help you improve performances of your request. You can specify which data you want to keep in your model or let the client request what he wants.

Installation

In order to install Lighter, just use compose:

composer require "avertys/lighter"

How to use Lighter

On model

To use lighter on your model you have to add Avertys\Lighter\LighterTrait to your model

class User extends Model
{
    use LighterTrait;

    protected $appends = ['fullname'];
}

Then, you just have to write the followinf code. In this example, accessors are not calculated.

$user = User::find(1);

return response()->json(
    $user->lighter()->keep(['name', 'address']);
, 200);

/*
{
    "name" : "Steve",
    "age" : "28" 
}
*/

You also can use lighter on collection with the helper.

$users = User::all();

return response()->json(
    lighter($users)->keep(['name', 'address']);
, 200);

/*
{
    "name" : "Steve",
    "age" : "28" 
},
{
    "name" : "John",
    "age" : "35" 
},
*/

Helper can be use on model

$user = User::find(1);

return response()->json(
    lighter($user)->keep(['name', 'address']);
, 200);

/*
{
    "name" : "Steve",
    "age" : "28" 
},
{
    "name" : "John",
    "age" : "35" 
},
*/

Using the request : Let your client request what he needs.

In the params, add as _keep parameter http://localhost/users?_keep["name"] .

$user = User::find(1);

return response()->json(
    lighter($user)->keep();
, 200);

/*
{
    "name" : "Steve",
    "age" : "28" 
},
{
    "name" : "John",
    "age" : "35" 
},
*/

Comments