fbsouzas / flery-builder
一个 Laravel 查询构建器包。
v0.1.0
2021-06-13 17:11 UTC
Requires
- php: >=7.4
- illuminate/database: ^8.29
Requires (Dev)
- infection/infection: ^0.21.4
- phpmd/phpmd: ^2.10
- phpstan/phpstan: ^0.12.88
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
This package is not auto-updated.
Last update: 2024-09-23 11:27:35 UTC
README
一个用于编写简单且灵活的 Eloquent 查询构建器的 Laravel 包。
安装
composer install fbsouzas/flery-builder
用法
<?php use Fbsouzas\FleryBuilder\FleryBuilder; use Illuminate\Http\Request; return FleryBuilder::to(User::class) ->apply($request->all()) ->get(); // or return FleryBuilder::to(User::class) ->apply([ 'where' => [ 'name' => 'Joe Doe', ], ]) ->get();
您可以使用 FleryBuilder 与其他 Laravel 查询构建器功能结合使用。
例如
<?php use Fbsouzas\FleryBuilder\FleryBuilder; use Illuminate\Http\Request; return FleryBuilder::to(User::class) ->apply($request->all()) ->where(['name' => 'Joe Doe']) ->get(); // or return FleryBuilder::to(User::class) ->apply($request->all()) ->join('contacts', function ($join) { $join->on('users.id', '=', 'contacts.user_id') ->where('contacts.user_id', '>', 5); })) ->get(); // or return FleryBuilder::to(User::class) ->apply($request->all()) ->whereJsonContains('options->languages', ['en', 'br']) ->get(); // and etc.
示例
选择
// That will return only the user's first name. GET /api/users?fields=first_name // or // That will return only the user's first name and last name. GET /api/users?fields=first_name,last_name
与
// That will return the user and his contact information. GET /api/users?with=contact // or // That will return the user and his contact information and posts. GET /api/users?with=contact;posts // or // That will return the user and just his posts title. GET /api/users?with=posts:id,title
类似
// That will return all users that have joe in their first names. GET /api/users?search[first_name]=joe
按顺序排列
// That will return a user's list ascendant ordered by the first name. GET /api/users?sort=first_name // or // That will return a user's list descendant ordered by the first name. GET /api/users?sort=-first_name
要求
- 此包需要 PHP 7.4 或更高版本。
测试
composer test
致谢
*此包受到 Spatie 的 Laravel 查询构建器 的启发
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。