Make easy query builder / eloquent queries based on requests for API Response
cacing69/bitbuilder is a Laravel package for make easy query builder / eloquent queries based on requests for api response.
It currently has 1 GitHub stars and 66 downloads on Packagist (latest version v0.3.5).
Install it with composer require cacing69/bitbuilder.
Discover more Laravel packages by cacing69
or browse all Laravel packages to compare alternatives.
Last updated
This package has feature to filter, sort and include relations based on request. BITBuilder can be applied on Laravel's default Eloquent builder & Query Builder. Query parameter names follow the JSON API specification.
composer require cacing69/bitbuilder
use App\Models\User;
use Cacing69\BITBuilder\FactoryBuilder;
use Cacing69\BITBuilder\Filterable;
use Cacing69\BITBuilder\Sortable;
...
public function index(Request $request)
{
$factory = new FactoryBuilder();
$data = $factory->on(User::class)
->addFilters([
// do filter on db based on request ex : url?filter[id]=1 will generate query where user_id = 1
Filterable::exact("id", "user_id"), // fill second parameter, if u want to make alias on request
// do filter on db based on request ex : url?filter[foo]=lorem
Filterable::like("foo", "column_foo", Filterable::LIKE_BEGIN) // generate query where column_foo like "lorem%"
// OR Filterable::like("fist_name", "user_first_name", Filterable::LIKE_END) // generate query where column_foo like "%lorem"
// OR Filterable::like("fist_name", "user_first_name", Filterable::LIKE_BEGIN_END) // generate query where column_foo like "%lorem%"
])
->addSorts([
Sortable::field('id', 'user_id') // will allow to sort via request url?sort_by=id (DESC), url?sort_by=-id (ASC)
])
->defaultSort("user_id") // will sort user by user_id DESC, u can use -user_id to make sort by ASC
->limit(20) // add limitation u can call force to show all data with method $obj->showAllData(true);
->with("media") // call eloquent relation
->where("user_blocked", 1) // add wehre clause on User
->setCursor("last_id"); // optional if u want to cursor pagination
->get(); // u can paginate also there -> paginate(20);
}
use DB;
use Cacing69\BITBuilder\FactoryBuilder;
use Cacing69\BITBuilder\Filterable;
use Cacing69\BITBuilder\Sortable;
...
public function index(Request $request)
{
$factory = new FactoryBuilder();
$data = $factory->on(DB::table("t_user"))
... // same as before
}
You can help me fix the structure in this package, because I made this in a very short time for necessary purposes, any PR very welcome here, thank you
The MIT License (MIT). Please see License File for more information.