Package Data | |
---|---|
Maintainer Username: | jedrzej |
Maintainer Contact: | jedrzej.kurylo@gmail.com (Jędrzej Kuryło) |
Package Create Date: | 2015-08-17 |
Package Last Update: | 2019-10-04 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-19 03:20:42 |
Package Statistics | |
---|---|
Total Downloads: | 144,835 |
Monthly Downloads: | 2,197 |
Daily Downloads: | 61 |
Total Stars: | 104 |
Total Watchers: | 3 |
Total Forks: | 7 |
Total Open Issues: | 1 |
Does your API need to allow filtering results? Of course it does!
Do you need to sort the results or return selected relations together with your model? I bet you do!
Let's do this!
Pimp your model!
class Post extends Eloquent {
use PimpableTrait;
}
Pimp your API!
class PostsController extends Controller {
public function index() {
return Post::pimp()->get();
}
}
What data do you need?
I need all the posts in a thread with ID 42...
...that contain a string "awesome"...
...that are no older than March 2014...
...that have been posted by any user other than the one with ID 666.
One more thing... sort it so that I get active posts at the top...
Oh, and make active and inactive posts sorted by the date they were posted...
Did I mention I need the user data with every post?
Ready... Steady... Go!
GET /api/posts
?thread_id=42
&text=%awesome%
&created_at=(ge)201403
&user_id=!666
&sort[]=is_active,desc
&sort[]=created_at,desc
&with=user
BAM! Done! How cool is that!
Laravel 4/5 package that allows to dynamically filter, sort and eager load relations for your models using request parameters.
It combines the following packages:
It simplifies embedding them in your models and allows using all 3 of them with a single function call.
Add the following line to composer.json
file in your project:
"jedrzej/pimpable": "0.0.4"
or run the following in the commandline in your project's root folder:
composer require "jedrzej/pimpable" "0.0.4"
In order to pimp your model class, you need to import PimpableTrait into your model. This will internally import all 3 behaviours.
class Model extends Eloquent {
use PimpableTrait;
}
By default all model fields are searchable and sortable; all relations can be eagerly loaded by default as well. If you need to limit which fields can be filtered and sorted by and which relations can be loaded, see documentation of corresponding behaviour package.
Once you pimp your model, additional method pimp() will be available on the model that enables the additional behaviour. All criteria will be taken from the request automatically, but if you want to override the request parameters, you can pass the desired value to the pimp() method:
//override all parameters
return Model::pimp($filters, $sort, $relations)->get();
//override sorting criteria only
return Model::pimp(null, $sort)->get();
Information how to configure the behaviours using request parameters can be found in documentation of corresponding behaviour package.
If you are using sort
request parameter for other purpose, you can change the name of the parameter that will be
interpreted as sorting criteria by setting a $sortParameterName
property in your model, e.g.:
protected $sortParameterName = 'sortBy';
If you are using with
request parameter for other purpose, you can change the name of the parameter that will be
interpreted as a list of relations to load by setting a $withParameterName
property in your model, e.g.:
protected $withParameterName = 'relations';