avertys / lighter
Make your requests lighter
1.0.3
2020-12-14 21:52 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-11-13 15:19:27 UTC
README
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" }, */