zing / laravel-query-builder
Build Eloquent query from request.
Installs: 1 773
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.0
- illuminate/database: ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/http: ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- mockery/mockery: ~1.3.3 || ^1.4.2
- nunomaduro/larastan: ^1.0 || ^2.0
- orchestra/testbench: ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.3.3 || ^10.0
- symfony/http-foundation: ^5.1.3 || ^6.0 || ^7.0
- zing/coding-standard: ^6.4 || ^7.0
- 3.x-dev
- 3.2.0
- 3.1.0
- 3.0.0
- 2.x-dev
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0
- 1.0.0-alpha.1
- 0.6.0
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-dependabot/github_actions/codecov/codecov-action-5
- dev-zingimmick-patch-1
This package is auto-updated.
Last update: 2024-11-14 19:36:22 UTC
README
Requirements
Require Laravel Query Builder using Composer:
composer require zing/laravel-query-builder
Usage
Basic usage
use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?search=Harry&status=1,2,3&desc=created_at&per_page=10 // sql: select * from "users" where ("name" like "%Harry%") and "status" in ("1", "2", "3") order by "created_at" desc limit 11 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->searchable(['name']) ->enableFilters(['is_visible', 'status']) ->enableSorts(['created_at']) ->enablePaginator() ->simplePaginate();
Search
use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?search=Harry // sql: select * from "users" where ("name" like "%Harry%" or "email" like "%Harry%") limit 16 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->searchable(['name', 'email']) ->simplePaginate();
Composite search
⚠️ The filter with default value is not supported yet.
use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?search=2021 // sql: select * from "users" where ("number" like "%2021%" or ("id" = "2021")) limit 16 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->searchable(['number', Filter::exact('encoded_id', 'id')]) ->simplePaginate();
Filter
use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?name=Harry // sql: select * from "users" where "name" like "%Harry%" limit 16 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->enableFilters([Filter::partial('name')]) ->simplePaginate();
use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?status=1,2,3 // sql: select * from "users" where "status" in ("1", "2", "3") limit 16 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->enableFilters([Filter::exact('status')]) ->simplePaginate();
use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?visible=1 // sql: select * from "users" where "is_visible" = true limit 16 offset 0 // uri: /api/users // sql: select * from "users" where "is_visible" = true limit 16 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->enableFilters([Filter::scope('visible')->default(true)]) ->simplePaginate();
Typed filter
⚠️ The filter with default value is not supported yet.
use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\Order; // uri: /api/users?search_type=number&search_value=2021 // sql: select * from "orders" where "number" like "%2021%" limit 16 offset 0 QueryBuilder::fromBuilder(Order::class, $request) ->enableTypedFilter( 'search_type', 'search_value', [Filter::partial('number'), Filter::partial('user_name', 'user.name')] ) ->simplePaginate();
Flagged filter
use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\Order; // uri: /api/users?number=2021&user_name=Jone // sql: select * from "orders" where (("number" like "%2021%") or (exists (select * from "users" where "orders"."user_id" = "users"."id" and "users"."name" like "%Jone%"))) limit 16 offset 0 QueryBuilder::fromBuilder(Order::class, $request) ->enableFlaggedFilter([Filter::partial('number'), Filter::partial('user_name', 'user.name')]) ->simplePaginate();
Cast Input(Skip auto cast)
use Zing\QueryBuilder\Enums\CastType; use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?is_visible=true // sql: select * from "users" where "is_visible" = true limit 16 offset 0 // uri: /api/users?is_visible=true,false // sql: select * from "users" where "is_visible" in (true, false) limit 16 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->enableFilters(Filter::exact('is_visible')->withCast(CastType::BOOLEAN)) ->simplePaginate();
use Zing\QueryBuilder\Enums\CastType; use Zing\QueryBuilder\Filter; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\Order; // uri: /api/orders?content=code,and // sql: select * from "orders" where "content" like "%code,and%" limit 16 offset 0 QueryBuilder::fromBuilder(Order::class, $request) ->enableFilters(Filter::partial('content')->withCast(CastType::ORIGINAL)) ->simplePaginate();
Sort
use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Sort; use Zing\QueryBuilder\Tests\Models\Order; // uri: /api/users?desc=created_date // sql: select * from "orders" order by "created_at" desc limit 16 offset 0 QueryBuilder::fromBuilder(Order::class, $request) ->enableSorts([Sort::field('created_date', 'created_at')]) ->simplePaginate();
Paginator
use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?size=5 // sql: select * from "users" limit 6 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->enablePaginator('size') ->simplePaginate();
use Zing\QueryBuilder\Paginator; use Zing\QueryBuilder\QueryBuilder; use Zing\QueryBuilder\Tests\Models\User; // uri: /api/users?size= // sql: select * from "users" limit 6 offset 0 QueryBuilder::fromBuilder(User::class, $request) ->enablePaginator(Paginator::name('size')->default(5)) ->simplePaginate();
License
Laravel Query Builder is an open-sourced software licensed under the MIT license.